0

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

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Jugortha
0

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");

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.