XML database in C # .net

I am developing a WPF client program for some websites. It uses an XML database. I am new to XML. Someone please explain how to create, add (most important), edit, read and encrypt an XML file. This is a big question, I know. But, this is urgent. Shut down as soon as possible. Searching the internet without getting the correct information.

+2


a source to share


5 answers


You should seriously consider using DataSet in your application and load data from 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 your database.



Here is an article on CodeProject that discusses using XML as a database:
Xml Database Demo

I know you tagged this C # question, but unfortunately the demo app is written in VB.NET.

+6


a source


(in response to your comment on Gerri's answer)

XML is inherently unacceptable. A valid XML document requires one document element. To "add", you must be able to go back to the closing tag of a document element and overwrite it. The only option is to read the entire document and write it back again. Also you can use XmlDocument or XDocument instead of XmlWriter which is a terribly painful API when you don't need very fine grained control over behavior.



The point is, XML makes for a really terrible database format. There are other lightweight database solutions out there that don't require a database server.

+3


a source


Because of the history of every company with a database that has a "standard" interface that no other company follows, XML has become the de facto way to transfer data between databases.

If it's used as intended, that's okay, as it needs to write in this format multiple times. There are many problems with writing XML using .NET as it has many ways to forget to finish writing by leaving the tags open (always use / flush / close). Warning: the more processor cores, the more often. Use Thread.BeginCriticalRegion () / Thread.EndCriticalRegion () if you have more than four real cores. Also, as suggested, it is better to save the earlier version as .bak or such.

Of course, if XML standards can be declared a "set of documents", we could add a document every time, and life would be much easier.

+1


a source


Load XML as XMLDocument .

0


a source


Assuming your database is small enough to be easily loaded into memory.

Create classes that model your database.

Add attributes to them DataContract

to indicate how you want to serialize them.

Use DataContractSerializer

to serialize the database to XML and then save it to disk.

Every time you update the database:

  • Create a new file as .tmp
  • Delete any old file named .old
  • Rename .xml to .old
  • Rename the new file from .tmp to .xml

When you download the file, if the .xml is corrupted or missing try .tmp

This will help you cope with the inevitable corruption that will arise during writing when something goes wrong.

0


a source







All Articles