1

这是xml的示例:

<root>
<something1></something1>
<something2></something2>
<something3></something3>
<important>
<some1>123_random_number</some1><some2>ASD</some2>
</important>
<important>
<some1>456_random_number</some1><some2>FGH</some2>
</important>
<important>
<some1>789_random_number</some1><some2>JKL</some2>
</important>
</root>

我需要更改元素的顺序,以获取这样的 xml 文档:

<root>
<something1></something1>
<something2></something2>
<something3></something3>
<important>
<some1>789_random_number</some1><some2>JKL</some2>
</important>
<important>
<some1>456_random_number</some1><some2>FGH</some2>
</important>
<important>
<some1>123_random_number</some1><some2>ASD</some2>
</important>
</root>

有人可以帮助我吗?在此先感谢,

4

1 回答 1

0

这很容易,但我是初学者:) 这是答案:

XmlReader reader = XmlReader.Create(@"dataIn.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNodeList elem = doc.GetElementsByTagName("important");

List<string> originalOrder = new List<string>();

foreach (XmlNode tag in elem)
  {
    originalOrder.Add(tag.InnerXml);
  }

int numberOfNodes = elem.Count;

for (int i = 0; i < numberOfNodes; i++) {
    elem[i].InnerXml = order[order.Count - 1 - i];
  }
doc.Save(@"dataOut.xml");
于 2013-07-07T17:12:11.033 回答