I am developing a WPF client program for some websites. It uses XML database. I am new to XML. Would someone please explain how to create,append(Most important),edit,read&encrypt XML file. It is a big question,i know . But, it is urgent.Have to complete the work ASAP. Searched in the internet, not getting correct info.
(in response to your comment on Gerri's answer)
XML is inherently not appendable. A valid XML document requires a single document element. In order to "append" you would need to be able to back over the closing tag of the document element and overwrite it. The only option is to read in the entire document and write it back out again. Also you may want to use XmlDocument or XDocument instead of XmlWriter which is a horribly painful API when you don't need very fine grained control over the behavior.
The fact is, XML makes a really terrible database format. There's other lightweight database solutions out there that don't require a database server.
You should seriously consider using a DataSet within your application and load up your data from an XML file via DataSet,ReadXml. When you're done with your updates write your changes using DataSet.WriteXml.
But you should also seriously consider not using XML as a database.
Here's an article on CodeProject that discusses using XML as a database:
Xml Database Demo
I know you tagged this question C# but unfortunately the demo app is written in VB.NET.
Assuming your database is small enough that you can easily load it into memory.
Create classes that model your database.
Add DataContract
attributes to them to indicate how you want them serialized.
Use DataContractSerializer
to serialize your database to XML and then save it to disk.
Each time you update the database:
- Create new file as .tmp
- Delete any old file called .old
- Rename .xml to .old
- Rename new file from .tmp to .xml
When you go to load the file, if .xml is corrupt or missing, try .tmp
This will help you survive the inevitable corruption that will occur during writing when something goes wrong.