Harry
Harry

Reputation: 338

XML File as repository

I am developing a WPF application using FW 4.0 for which I need to save the application data i.e. (Setting) in a xml file.

I have used 2.0 earlier I was wondering what is the best approach to create xml file and then able to modify (append, add new element and delete) in the xml file. The XML file will have many Elements and child elements.

Can some one point me to a link/suggest approach for creating a generic method which could be called for add/modify/delete the Element.

Upvotes: 0

Views: 219

Answers (2)

Konrad Morawski
Konrad Morawski

Reputation: 8404

Personally I would recommend you creating a class to represent your settings and then use XML serialization for saving and retrieving them.

Handling it as raw XML isn't recommendable, you're likely to end up with ugly code, difficult to maintain.

A quick start tutorial on the subject of XML serialization in C#: http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization

If you have your reasons to go with raw XML, however, then LINQ to XML recommended by Petr Abdulin is a good choice.

As a side note - LINQ to XML is only recommendable for small or medium-sized XML files (thereby suitable for purposes of storing application settings), but it should not be used for handling big files. LINQ to XML reads in and parses XML files in their entirety. If they're large, it's better to use an XmlReader and process them sequentially.

Upvotes: 0

pabdulin
pabdulin

Reputation: 35255

I find LINQ to XML one of the most convenient and clear way to handle XML in .NET 4.0 (and it's also available in .NET 3.5).

Upvotes: 1

Related Questions