开发者

Updating XML file c#

开发者 https://www.devze.com 2022-12-29 23:32 出处:网络
I want to replace a xml node that is not a child of the root element. how c开发者_高级运维an I do it?If you use .Net 3.5 you can use System.Xml.Linq:

I want to replace a xml node that is not a child of the root element. how c开发者_高级运维an I do it?


If you use .Net 3.5 you can use System.Xml.Linq:

XDocument x = XDocument.Load("filename.xml");
x.Root.GetElement("ElementName").SetValue("value");
x.Save("filename.xml");


You could use the XmlDocument.ImportNode, and XmlElement.AppendChild to do this.

public void AppendNodeToTarget(XmlElement targetElement, 
    XmlDocument targetDocument, XmlNode node)
{
    XmlNode nodeToAppend = targetDocument.ImportNode(node, true);
    targetElement.AppendChild(nodeToAppend);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号