Updating XML file c#
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);
}
精彩评论