0

the XmlReader has following content:

<ns0:Fields>
  <omm:Field DataType="Utf8String" Name="ROW80_3">
    <omm:Utf8String> Latam News </omm:Utf8String>
  </omm:Field>
  <omm:Field DataType="Int32" Name="RECORDTYPE">
    <omm:Int32>228</omm:Int32>
  </omm:Field>
  <omm:Field DataType="Utf8String" Name="ROW80_4">
    <omm:Utf8String>ATDNEWSRUS</omm:Utf8String>
  </omm:Field>
  <omm:Field DataType="Utf8String" Name="ROW80_1">
    <omm:Utf8String>12:28 27JUN09    PRODUCT LIST</omm:Utf8String>
  </omm:Field>
  <omm:Field DataType="Utf8String" Name="ROW80_2">
    <omm:Utf8String>ATDNEWSLATAM</omm:Utf8String>
  </omm:Field>
  <omm:Field DataType="Utf8String" Name="BQOS">
    <omm:Utf8String>0</omm:Utf8String>
  </omm:Field>
</ns0:Fields>

How can I rearrange the elements to start at ROW80_1 and end at ROW80_4.

dax
  • 10,779
  • 8
  • 51
  • 86

3 Answers3

5

If you can use XLinq, things become much simpler:

XDocument doc = XDocument.Parse(@"..."); 

var children = doc.Elements().Single().Elements().OrderBy(element => (string) element.Attribute("Name"));

var newRoot = new XElement(((XNamespace) "yourNamespaceHere") + "Fields",
 new XAttribute(XNamespace.Xmlns + "nso", "yourNamespaceHere"),
 new XAttribute(XNamespace.Xmlns + "omm", "otherNamespace"),
 children);

var newDocument = new XDocument(newRoot);
Console.WriteLine(newDocument); 

You can also filter out elements.

pn.
  • 852
  • 1
  • 8
  • 9
0

The XmlReader cannot rearrange elements. However, depending where you load them to (e.g. XmlDocument etc.), you can rearrange them afterwards.

Lucero
  • 59,176
  • 9
  • 122
  • 152
0

As Lucero has mentioned you can't re-arrange the Xml document using the XmlReader class, however it is possible to rearrange the data contained with the document logically by using a temporary sortable data store generic dictionary or recreating the documents with all of your elements rearranged.

chinna
  • 535
  • 1
  • 6
  • 15