Freitag, 14. Dezember 2007

System.XML how to store data the easy way

Nearly every application has to story data. Some bigger apps use powerful databases other use text files. One newer thing is storing your Data in XML files, which is a pretty good thing for middle size data.

Okay sure you could realize application storage by the use of .net's application Settings but, then the data is lost if your changing the application version. You are also not able the export date out of the settings.

System.Xml provides very good Classes for developing an XML Storage System.

First we are looking on the System.Xml.XmlTextWriter Class. In the showed scenario we are writing data from a ListView - to be more precisely it is a favourites list for websites

Imports System.Windows.Forms
Imports System.Xml

Public Class FavoritenExport
Private Sub Save()
Dim wtrXML As New XmlTextWriter(Path, System.Text.Encoding.UTF8)
With wtrXML
.Formatting = Formatting.Indented
.WriteStartDocument()
.WriteComment("Favourite file")
.WriteStartElement("WebSites")
Dim objListViewItem As New ListViewItem
For Each objListViewItem In lvwWebSites.Items
.WriteStartElement("WebSite")
.WriteElementString("Name", objListViewItem.Text)
.WriteElementString("Category", objListViewItem.SubItems(1).Text)
.WriteElementString("URL", objListViewItem.SubItems(2).Text)
.WriteEndElement()
Next
.WriteEndElement()
.WriteEndDocument()
.Flush()
.Close()
End With
Me.Close() 'Closes the form
End Sub
End
Class











What does Save Exactly? First we are creating our XmlTextWriter here named "wtrXML". The Variable Path just represents the path where the file should be written into e. g. C:\test.xml or C:\test.fda (the extension don't have to be xml). System.Text.Encoding.UTF8 is the charset, it is possible to use other ones, but UTF8 is the most used for XML files.









The result of such an XML File could be like this one:






<?xml version="1.0" encoding="utf-8"?>

<!--Favourite file-->


<WebSites>


<WebSite>


<Name>A Guided Tour of Windows Presentation Foundation</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://msdn2.microsoft.com/en-us/library/aa480221.aspx</URL>

</WebSite>


<WebSite>


<Name>C# Home : News</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://www.csharp-home.com</URL>

</WebSite>


<WebSite>


<Name>Channel 10</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://on10.net/</URL>

</WebSite>


<WebSite>


<Name>CodePlex</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://www.codeplex.com/Default.aspx</URL>

</WebSite>


</WebSites>



So what is important?



First you have do declare your XmlTextwriter, then you let it write down the start document stuff. After that your storage logic have to be implemented. In this case we use a root element named 'WebSites' this is not needed, but if you store more things which are not as closely related as it is in this case this could be an usefull technique to separate the different parts.



In this root Element we put the real data - our websites.  Because we do not only have on website, or do not know how much websites are in the listview by coding the writer, we use for each. This loop starts with a new StartElement, this time we call it for 'Website', after that the real data is stored by calling WriteElement string. The first parameter indicates the name the second the value of the element. Then WriteEndElement is called for Closing the WebseiteElement. After the for each loop WriteEndElement closes the 'WebSites' Element,  WriteEndDocument closes the docuement.


Next post will show how to read Xml files using System.Xml.XmlTextReader.

1 Kommentar:

Souvergin hat gesagt…

It is bad that, the blogsystem does not show the tabs in the XML file. Which the Writer makes ;)