[英]How to replace element of xml using xdocument in c#
example xml:示例xml:
<string-name>
<given-name>Sisgon</given-name>
</string-name>
changes of xml element: xml元素的变化:
<string-name>
<surname>Sisgon</surname>
</string-name>
I want to change the given-name tag to surname without changing the inner text.我想在不更改内部文本的情况下将given-name 标签更改为姓氏。
How about this这个怎么样
XDocument xmlDoc = XDocument.Parse(content);
var event_nodes = xmlDoc.Descendants("given-name");
foreach(var node in event_nodes)
{
node.Name = "surname";
}
System.Diagnostics.Debug.WriteLine(xmlDoc.ToString());
To add an attribute add the following in the for each:要添加属性,请在 for each 中添加以下内容:
XAttribute attribute = new XAttribute("Name","value");
node.Add(attribute)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.