tags:

views:

66

answers:

4

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"&gt; 

       <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"&gt;

       <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.

+2  A: 

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

Marc Gravell
/seconded - the whole point of XML is that things like this are non-issues
annakata
@Marc: The order *might* be important when signing an XML document / validating a digital signature. There is a W3C recommendation called Canonical XML (http://www.w3.org/TR/xml-c14n.html) to handle specifically such use cases.
0xA3
@divo - hence the question; *if* that is the problem, it is worth effort. If it is purely cosmetic, it probably isn't.
Marc Gravell
In XML Digital Signature, it is the canonical XML that is signed, not the original source.
John Saunders
@John: Yes, that is obviously true. But the OP didn't give a reason why the order matters to him, so he might just want to do that?
0xA3
@Marc I agree, but in this case my client as said the order is important.
Dan
A: 

Would XmlWriter be an option for you?

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

0xA3
Indeed (+1); since it is one-way, it *can't* do anything other than push things out in the order you supply them. A very different beast, though.
Marc Gravell
Is this is the only way to achieve this? Iv already completed the document creation which is quite involved, dont really want to have to re-do using xmlwriter
Dan
If attribute order is important, what your client is asking you to produce is not really XML. So you'll need to take your own steps to serialise it in whatever way is specified by your client.
bobince
@Dan, you don't have to redo your work. You could also write your XDocument to a filtering XmlWriter which would do a post-processing on your XML stream.
0xA3
A: 

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!

Brian Agnew
Thanks, its just the ordering is a requirement of the organisation im submitting the xml too.
Dan
Mmm. Sounds like they have a problem at their end :-) You may want to see if you can influence that
Brian Agnew
A: 

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.

Robert Rossney