4

I learning Xml data handling 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 to add/edit/delete new books to the XML file. Could you please guide me on what all classes to explore for these functionality. I find lot of classes like XmlDocument XmlTextWriter etc. Some sites suggest to use LINQ as well. I am confused as to which was to go. Is there any good material I can refer to understand this.

Leany
  • 159
  • 1
  • 2
  • 5

2 Answers2

6

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're comfortable with the technology. LINQ to SQL seems a bit easier, in my opinion.

alex
  • 3,710
  • 32
  • 44
  • That really helped. Thanks! I have another query. While iterating through the xDocument, I write the following to get to the root node. It is working fine, but not sure if it is the right way. `XDocument xDoc = XDocument.Load("data.xml"); XElement rootNode = (XElement)xDoc.FirstNode;` – Leany May 12 '11 at 09:41
  • Then I am doing `foreach (XElement book in rootNode.Nodes())` – Leany May 12 '11 at 09:42
  • 1
    @Leany: cut out the middleman. You don't really need XDocument at all, simply use `XElement rootNode = XElement.Load("data.xml")` – alex May 12 '11 at 09:47
  • oh, that worked! I replaced `foreach (XElement book in rootNode.Nodes())` with `foreach (XElement book in rootNode.Elements())`. So, is `XNode` an alternative to `XElement`? – Leany May 12 '11 at 10:05
  • 1
    @Leany: You can read more about them [here](http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/086c5273-32eb-48d7-999f-74e31e6e531f/) – alex May 12 '11 at 10:07
  • @alex thanks. Is it possible to do edit a book with a specific name using LINQ in a similar way you did for delete? – Leany May 12 '11 at 10:20
  • I mean, get an XElement that matches and change value. – Leany May 12 '11 at 10:22
  • @Leany: I edited my answer with a way to edit an existing element. – alex May 12 '11 at 10:33
1

If the file is reasonable small - i.e. not several MB in size - you should use either XmlDocument (the classic way) or XDocument (the new LINQ classes for XML processing). You will find lots of examples for both.
The following search results might give a hint as to when you should use which of those 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.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443