Controlling the order of XML namepaces

前端 未结 6 1160
你的背包
你的背包 2021-01-15 00:26

I\'m having a problem getting the \"xmlns\" to appear first in the root attribute list.

Im getting this:

  

        
相关标签:
6条回答
  • 2021-01-15 00:58

    I have a customer with this very problem. This was a real pain in the s, so I wrote a workaround to solve this.

    Please note this is not a beautiful solution, and this should be not encouraged, but works.

    public static class MyKludgeXmlClass
    {
        public static XmlDocument CreateXmlDocumentWithOrderedNamespaces()
        {
            var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MyRoot xmlns=\"http://www.example.com/schemas/1.0/VRSync\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.example.com/schemas/1.0/VRSync http://xml.example.com/vrsync.xsd\"></MyRoot>";
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xml);
    
            return doc;
        }
    }
    

    With XmlDocument you can retrieve the root:

    var xmlDoc = MyKludgeXmlClass.CreateXmlDocumentWithOrderedNamespaces();
    XmlElement root = xmlDoc.DocumentElement;
    

    And append children nodes using your favorite method.

    0 讨论(0)
  • 2021-01-15 01:01

    Attribute ordering is NOT specified in the XML document, and shouldn't be relied upon. It may be worth looking at the spec

    You'll find that if you read a XML document into a DOM, and write it out, regardless of the platform/library, you can't (and shouldn't) rely on the attribute ordering. It's a common misconception, btw!

    0 讨论(0)
  • 2021-01-15 01:07

    Would XmlWriter be an option for you?

    Afaik, it gives you full control of the order of attributes and namespace declarations.

    0 讨论(0)
  • 2021-01-15 01:07

    Because sometimes the right answer is to say, no, don't do that...

    Per W3C Namespaces in XML Recommendation, section 3 Declaring Namespaces:

    [Definition: A namespace (or more precisely, a namespace binding) is declared using a family of reserved attributes. Such an attribute's name must either be xmlns or begin xmlns:. These attributes, like any other XML attributes, may be provided directly or by default. ]

    Therefore, the order of namespace declarations, like the order of any attributes, is insignificant.

    So, no conformant XML tool or library will care about the order of namespace declarations, and neither should you.

    0 讨论(0)
  • 2021-01-15 01:10

    Software that requires attributes to be in a specified order doesn't conform to the XML recommendation.

    The first question you should be asking is not, "How can I produce XML with namespace attributes in a defined order?" Instead, it should be, "What are the other respects in which this software doesn't conform to the XML recommendation?" Because I will bet you one crisp new American dollar that if the recipient's process violates the XML recommendation in one respect, it violates it in at least one other.

    0 讨论(0)
  • 2021-01-15 01:11

    IIRC, the order of attributes (in xml) is unimportant... so why change it? Is it causing an actual problem?

    0 讨论(0)
提交回复
热议问题