我有一个xml文件,我需要交换几个标签。 例如:在下面的示例中,我想交换元素(B和C及其子元素)并将其更新回相同的xml。
<A>
<B>
<xxx>
<zzz>
.
</B>
<C>
<nnn>
<mmm>
.
</C>
</A>
所以最后它应该是下面的内容。
<A>
<C>
<nnn>
<mmm>
.
</C>
<B>
<xxx>
<zzz>
.
</B>
</A>
阅读+解析+保存回来xml有什么帮助吗?
答案 0 :(得分:1)
抱歉VB。创建测试XML的代码不会相同,但是在C之后移动A的代码应该是关闭的。
Dim xe As XElement
' to load from a file - similar to C#
' Dim yourpath As String = "your path here"
'xe = XElement.Load(yourpath)
' for testing
xe = <A>
<B>
<xxx></xxx>
<zzz></zzz>
b stuff
</B>
<C>
<nnn></nnn>
<mmm></mmm>
c stuff
</C>
</A>
'this should be close to what the C# will look like
Dim holdB As XElement = New XElement(xe.<B>.FirstOrDefault) 'create copy of the B element
xe.<B>.Remove() 'remove B
xe.<C>.FirstOrDefault.AddAfterSelf(holdB) 'add after C
' to save file - similar to C#
' xe.Save(yourpath)
编辑:已转换的部分
XElement xe = default(XElement);
// to load from a file - similar to C#
string yourpath = "your path here";
xe = XElement.Load(yourpath);
XElement holdB = new XElement(xe.FirstOrDefault);
//create copy of the B element
xe.Remove();
//remove B
xe.FirstOrDefault.AddAfterSelf(holdB);
//add after C
// to save file - similar to C#
xe.Save(yourpath);
//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================