tags:

views:

123

answers:

4

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.

A: 

Load your XML as an XMLDocument.

Gerrie Schenck
I am using writestartelement,writestring and writeendelement for writing user info into xml file.But i am not able to append new user info.Can you help?
+3  A: 

(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.

Josh Einstein
Actually the statement "it uses XML database" shows the poster has no clue what a database supposedly is to do. A file is not a atabase by any means, unless there is a lot of support there. And - as you said - XML makes a really crappy database format ;)
TomTom
Yeah in fact I'll just mention one other reason why he shouldn't use XML... About once every week or two, Jing (a useful application nonetheless) will complain that it's config file is trashed. It's probably some junk at the bottom of the file that makes it malformed. But guess what... because it's not valid XML it hoses the entire file. Wouldn't that be awesome if that happened 73% into a 40 MB file?
Josh Einstein
+2  A: 

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.

Jay Riggs
I am not at all authorised to change that decision to use XML DB.But, I won't ever choose it in the future.
Is there any helpful link that explains XML operations using Dataset?
There wouldn't be any XML operations using the DataSet. The data is read into a DataSet from your XML file, Once in the DataSet do DataSet operations on the data; the DataSet doesn't know or care its data came from XML. When you're done, write your DataSet back to XML using DataSet.WriteXml.
Jay Riggs
A: 

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:

  1. Create new file as .tmp
  2. Delete any old file called .old
  3. Rename .xml to .old
  4. 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.

Hightechrider