开发者

XML Database in C#.net

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 u开发者_如何学JAVArgent.Have to complete the work ASAP. Searched in the internet, not getting correct info.


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.


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


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.


Due to history of each data base company coming up with a "standard" interface that no other company follows, XML has become the defacto way to transfer data between databases.

If this in the intended use than it is fine as it only has to write in this format some times. There is a lot to worry about in writting XML using .NET as it has a lot of ways to forget to finish the writing leaving open tags (always use using/flush/close). Warning: The more processing cores the more often .Net screws up. Use Thread.BeginCriticalRegion()/Thread.EndCriticalRegion() if you have more than four real cores. Also as suggested it is best to save the earlier version as a .bak or such.

Of course if the XML standards could have a declaration of "document set" then we could append a document each time and life would be a lot easier.


Load your XML as an XMLDocument.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜