Tutorial :Controlling the order of XML namepaces



Question:

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

Im getting this:

  <myroot           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://www.someurl.com/ns/myroot http://www.someurl.com/xml/schemas/myschema.xsd"          xmlns="http://www.someurl.com/ns/myroot">            <sometag>somecontent</sometag>        </myroot>  

And i want this:

<myroot           xmlns="http://www.someurl.com/ns/myroot"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://www.someurl.com/ns/myroot http://www.someurl.com/xml/schemas/myschema.xsd">           <sometag>somecontent</sometag>        </myroot>  

My code looks like this:

  XNamespace rt = "http://www.someurl.com/ns/myroot";          XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";            var submissionNode = new XElement(XmlNameSpaces.rt + "myroot");          submissionNode.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));          submissionNode.Add(new XAttribute(xsi + "schemaLocation", @"http://www.someurl.com/ns/myroot http://www.someurl.com/xml/schemas/myschema.xsd"););  

What do i need to do different to change the order?

EDIT: I understand the order is not normally relavent, but its a requirement in this case.


Solution:1

Would XmlWriter be an option for you?

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


Solution:2

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


Solution:3

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!


Solution:4

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.


Solution:5

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.


Solution:6

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »