tags:

views:

188

answers:

3

Hi,

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.

Please advise.

Thank You - Eneo.

A: 

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

Lucero
A: 

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
+4  A: 

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
Great answer, +1.
unforgiven3