i have a project with xml,i must open a xml file in tree view,i've done this,but now
i want to add node.i have a existing file witch i've open,and in this xml file i need to insert new node,can someone help me with this.
thank you very much

Recommended Answers

All 3 Replies

Use System.XML namespace it helps a lot!

I guess you can download some xml parser from websites like sourceforge.

There are severals methods to add a node to your xml file including the use of the DOM(Document object models) methods so a propose this code I invite u to try it
suppose that we have an xml file

<books>
<A>
<B>book1</B>
<C>book2</C>
<D>book3</D>
</A>
</books>
we want add
<E>
<F>book4</F>
</E>
to the xml file
try to execute this method
public void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<books>" +
"<A>" +
"<B>book1</B>" +
"<C>book2</C>" +
"<D>book3</D>" +
"</A>" +
"</books>");


XmlNode root = doc.DocumentElement;


//Create  the E node.
XmlElement oNodeE = doc.CreateElement("E");
//Create the F node
XmlNode oNodeF = doc.CreateElement("F");
oNodeF.InnerText = "book4";
oNodeE.AppendChild(oNodeF);
//Add the node to the document.
root.InsertAfter(oNodeE, root.SelectSingleNode("//book/A"));
//Save the file contents
doc.Save("C:\\MyFile.xml");
MessageBox.Show("Ok");
}

There is not only this method of corse u can use other alternatives using xmlwriter, text writer
and within the .Net framework 3.5 there is xLing
library witch simplify a lot the achivement of such task

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.