更改xml中的节点位置 [英] change node place in xml

查看:86
本文介绍了更改xml中的节点位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个xml文件,例如:

< a>
< 1>文字1</1>
< 2>文字2</2>
< 3> Text3</3>
</a>

我在aspx页面上有两个按钮.当我在下拉列表中选择一个节点元素并单击向上按钮时,该节点将上升:

像:我在dropdownList中选择一个text3,然后单击向上按钮.之后,我希望我的xml文件如下所示:

< a>
< 1>文字1</1>
< 3> Text3</3>
< 2>文字2</2>
</a>


请帮助我.

在此先感谢

hi guys,

i have a xml file like:

<a>
<1>Text1</1>
<2>Text2</2>
<3>Text3</3>
</a>

i have a two button up and down in aspx page. when i select a node element in dropdownlist and click on up button then node is going up:

like: i select a text3 in dropdownList and click up button. after it i want my xml file look like this:

<a>
<1>Text1</1>
<3>Text3</3>
<2>Text2</2>
</a>


please help me in this.

thanks in advance

推荐答案

我希望这会起作用.


I hope this will work.


XmlDocument xmlDocument = new XmlDocument();
 xmlDocument.PreserveWhitespace = false;
 xmlDocument.Load(@"file.xml");
 
XmlNode fieldNode = xmlDocument.SelectSingleNode(
 @"/root/field[@name = ''3'']");
 
if (fieldNode != null) {
 fieldNode.ParentNode.InsertBefore(fieldNode,
 fieldNode.PreviousSibling);
 }
 
// save changes somewhere
 // in this example simply the console to show changes
 xmlDocument.Save(Console.Out);


来吧,您还是不需要它:"1","2"和"3"是无效的标记名,因此,这甚至不是一个很好的标记,格式的XML,将无法解析.

—SA
Come on, you don''t need it anyway: "1", "2" and "3" are invalid tag names, so this is not even a well-formed XML, won''t parse.

—SA


这篇关于更改xml中的节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆