hello there!

i have a problem how to write to a xml file using the xmlpath technology

i have the following file books.xml

<books>
<A property="a">
<B>text</B>
<C>textg</C>
<D>99999</D>
</A>
</books>

i need to add to the file it self nodes, for example the E & F nodes how can i do it?

<books>
<A property="a">
<B>text</B>
<C>textg</C>
<D>99999</D>
<E>
<F>0000</F>
</E>
</A>
</books>

Thnaks ahead

There are several methods to add a child node to an xml File I propose the this solution using DOM witch is the document object model if u work with framework 2.0
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book>" +
"<A property='a'>" +
"<B>text</B>" +
"<C>textg</C>" +
"<D>99999</D>" +
"</A>" +
"</book>");

XmlNode root = doc.DocumentElement;

//Create the E node.
XmlElement oNodeE = doc.CreateElement("E");
//Create the F node
XmlNode oNodeF = doc.CreateElement("F");
oNodeF.InnerText = "0000";
oNodeE.AppendChild(oNodeF);
//Add the node to the document.
root.InsertAfter(oNodeE, root.SelectSingleNode("//book/A"));

Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);


doc.Save("C:\\MyFile.xml");
MessageBox.Show("Ok");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.