简体   繁体   中英

How to the replace node names with new name and keep the attributes using C# and Linq to XML?

I need to change

<Test Language="English" Id="0" />

to

<Exam Language="English" Id="0" />

How to the replace node names with new name and keep the attributes ?

You can use Name property

var xdoc = XDocument.Load("input.xml");
var nodes=xdoc.Descendants("Test").ToList();//Get all "Test" node 
nodes.ForEach(d => d.Name = "Exam "); // Set name to 'Exam'
xdoc.Save("output.xml");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM