XML data management in .NET.

I am learning Xml data processing in .NET. I have the following XML 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 need to learn how to add / edit / delete new books in an XML file. Could you advise me that study all classes for these functions. I find many classes like XmlDocument XmlTextWriter etc. Some sites suggest using LINQ. I am embarrassed to have to go. Is there any good material that I can name to figure this out.

+3
source share
2 answers

Here's an example of adding and removing elements using LINQ to XML:

 // load the XML file into an XElement XElement xml = XElement.Load(filePath); // add a new book xml.Add( new XElement("BOOK", new XElement("TITLE", "book 3"), new XElement("AUTHOR", "author 3"), new XElement("PRICE", 0.1), new XElement("YEAR", 2012))); // remove a book that matches the desired title xml.Elements("BOOK").Where(x => x.Element("TITLE").Value == "book 1").Remove(); // to edit an existing element: xml.Elements("BOOK") .Single(x => x.Element("TITLE").Value == "book 2") // take a single book .Element("AUTHOR").Value = "new author"; // and change its author field 

Basically, use whatever you want as long as you are comfortable using the technology. LINQ to SQL seems a little easier in my opinion.

+4
source

If the file is reasonable, i.e. not MB size, you should use XmlDocument (the classic way) or XDocument (the new LINQ classes for XML processing). You will find many examples for both.
The following search results can give a hint about when you should use which of these classes: http://www.google.com/search?hl=en&q=XmlDocument%20vs.%20XDocument . Personally, I would suggest using XDocument because its API is easier to use.

+1
source

All Articles