I have a xml file whose few tags I need to swap. Eg: In below sample, i want to swap elements (B and C along with its sub-elements) and update it back to the same xml.
<A>
<B>
<xxx>
<zzz>
.
</B>
<C>
<nnn>
<mmm>
.
</C>
</A>
so finally it should be something like below.
<A>
<C>
<nnn>
<mmm>
.
</C>
<B>
<xxx>
<zzz>
.
</B>
</A>
Any help in reading + parsing + saving-back xml ?
Sorry about the VB. The code to create the test XML will not be the same but the code to move A after C should be close.
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)
edit: Converted parts
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
//=======================================================
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.