tags:

views:

76

answers:

3

I want to replace a xml node that is not a child of the root element. how can I do it?

+2  A: 

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");
Jouke van der Maas
and if it is not .Net 3.5?
Pila
+2  A: 

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);
}
code4life
A: 

If you want to know how to operate XML files, this link may help you. Xml operations