Adding a new node to an existing XmlDocument object

I have xml in the following format.

<BOOKS> <BOOK> <TITLE>book 1</TITLE> <AUTHOR>author 1</AUTHOR> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </BOOK> <BOOK> <TITLE>book 2</TITLE> <AUTHOR>author 2</AUTHOR> <PRICE>20.90</PRICE> <YEAR>1995</YEAR> </BOOK> </BOOKS> 

I have an Add(XmlDocument xDoc, Book newBook) method Add(XmlDocument xDoc, Book newBook) to add a new book to an XmlDocument object, which is passed to the Add(..) method. How can i do this.

+7
source share
1 answer
 XmlDocument doc = new XmlDocument(); doc.Load("file.xml"); XmlElement foo = doc.CreateElement("foo"); XmlElement bar = doc.CreateElement("bar"); bar.InnerText = "whatever"; foo.AppendChild(bar); doc.DocumentElement.AppendChild(foo); doc.Save("file.xml"); 

see Martin Honnen Publish to: Adding a New Node to an Existing XML Document

+24
source

All Articles