3
XmlSerializer serializer = new XmlSerializer(typeof(IxComment));
System.IO.StringWriter aStream = new System.IO.StringWriter();
serializer.Serialize(aStream,Comments);
commentsString = aStream.ToString();

Here the commentsString has the the following element in it

<IxComment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Is there any possibility to interchange the xsi and xsd attribute and get the element as shown below

<IxComment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

Will this cause any other issue?

EDIT: Why do i need to do this?

We are migrating an existing application from 1.1 to 3.0 and in the code there is a if loop

int iStartTagIndex = strXMLString.IndexOf("<IxComment xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");

that check for the index of the IxComment. Here the o/p of the serializer and the condition differ in the position of xsd and xsi. So i am trying to know whether we can instruct the serializer to provided the o/p as required.

I have another question here as this was an existing application does the serializer O/P differs with versions?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Gopi
  • 5,656
  • 22
  • 80
  • 146
  • 1
    I don't know if or how, but I'd like to ask _why_? Are you creating XML for a consumer that cannot handle XML (using string splitting or - god forbid - regular expressions instead)? – Benjamin Podszun Apr 05 '10 at 10:52

1 Answers1

3

I hope there's no way to affect the order of things that should not matter to any piece of code that understands XML. Any piece of code that has problems with the order of namespace declarations is badly broken and must be fixed, period.


After seeing your edit, I'm even more adamant: fix your broken code. Your code should never have performed string processing on XML. You should simply fix your code and not try to fix the XML standard, which dictates that the order of namespace declarations is not relevant.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Will the O/P of the XmlSerializer differs with versions? As i noticed the current IxComment element differs with one already existing in the DB – Gopi Apr 05 '10 at 11:12
  • 1
    +1 - My comment above already assumed that something like that is the case. OP: Fix the assumption. XML attributes are unordered. If your legacy code relies on a specific order, it's just plain wrong. Try to fix the real issue, without creating a workaround. – Benjamin Podszun Apr 06 '10 at 00:17
  • 2
    Exception is of course version control which is recognizing every xml file in my repo as changed the moment I switch machines :) Came across your answer while looking for an answer to this.... – sTodorov Apr 20 '17 at 13:33
  • 1
    @stod that's not an exception. Version control is processing them as text files, not XML. – John Saunders Apr 21 '17 at 20:14
  • The corollary of this answer is: XML is a binary format. I hate it with my heart. – Alberto Chiesa Apr 15 '22 at 13:33