Hr.SAAD
Hr.SAAD

Reputation: 35

Change the order of nodes in a Xml file

I would like to change odrer of some nodes in a XMl file. Could someone please tell me how I can implement that.

my Xml look like this :

<magicdraw>
<diagram>
<fragment>
<message id="AA22"/>
<message id="AF32"/>
<message id="CD52"/>
<combinedfragment id="LL43"/>
</fragment>
</diagram>
</magicdraw>

I wanna change it to:

<magicdraw>
<diagram>
<fragment>
<message id="AA22"/>
<combinedfragment id="LL43"/>
<message id="AF32"/>
<message id="CD52"/>
</fragment>
</diagram>
</magicdraw>

Upvotes: 0

Views: 1212

Answers (1)

Perazim
Perazim

Reputation: 1549

You can simply iterate through your list box items, identify the corresponding 'node' element and AppendChild it, that way you should end up with the proper order as doing AppendChild on a node already inserted somewhere moves it around and you would move any node to its proper position:

XmlDocument nodeDoc = new XmlDocument();
linksDoc.Load(Server.MapPath("App_Data/Node.xml"));


foreach (ListItem li in lb1.Items)
{
  string itemId = li.Value;

  XmlNode node = doc.SelectSingleNode(string.Format("/root/node[@id = '{0}']", itemId));
  if (node != null)
  {
    node.ParentNode.AppendChild(node);
  }
}

Upvotes: 1

Related Questions