I’m working on a project in which I have to save data I read from an XML file to a database, I’m already using code I made but it’s a bit primitive, I’m using XMLTextReader and a few other methods to navigate through the lines of code on the file and read the data I want, however I’ve read this would be so much “easier” if I used Serialization on my program, now I’ve seen a few examples of this and to be honest haven’t quite understood them, never even heard of them before, so if anyone could explain it to me in general terms and provide a little example on how I could apply it to my project it would be great.
This is what I have now:
For y As Integer = 0 To 167
Dim reader1 As XmlTextReader = New XmlTextReader(dir)
reader1.ReadStartElement("response")
reader1.ReadToNextSibling("hourly_forecast")
reader1.ReadStartElement("hourly_forecast")
reader1.ReadToNextSibling("forecast")
reader1.ReadStartElement("forecast")
CurrentLine= reader1.LineNumber
If CurrentLine= LastLine Then
reader1.ReadToNextSibling("forecast")
CurrentLine= reader1.LineNumber
End If
While reader1.LineNumber <= LastLine
For w As Integer = 0 To y
reader1.ReadToNextSibling("forecast")
CurrentLine= reader1.LineNumber
Next w
End While
LastLine = CurrentLine
For x As Integer = 1 To 2
Dim reader As XmlTextReader = New XmlTextReader(dir)
reader.ReadStartElement("response")
reader.ReadToNextSibling("hourly_forecast")
reader.ReadStartElement("hourly_forecast")
reader.ReadToNextSibling("forecast")
Do While reader.LineNumber < CurrentLine
reader.ReadToNextSibling("forecast")
Loop
reader.ReadStartElement("forecast")
If x = 1 Then
reader.ReadToNextSibling("FCTTIME")
reader.ReadStartElement("FCTTIME")
reader.ReadToNextSibling("hour")
values(y, x) = reader.ReadString()
d = values(y, x)
reader.ReadToNextSibling("year")
year = lector.ReadString()
reader.ReadToNextSibling("mon_padded")
month = reader.ReadString()
reader.ReadToNextSibling("mday_padded")
day= reader.ReadString()
a = year.Chars(2)
b = year.Chars(3)
c = a + b
x = x - 1
values(y, x) = day + "-" + month + "-" + c
x = x + 1
End If
If x = 2 Then
reader.ReadToNextSibling("temp")
reader.ReadStartElement("temp")
reader.ReadToNextSibling("metric")
values(y, x) = reader.ReadString()
End If
Next x
Next y
This is an example of what my XML looks like:
<response>
<version>0.1</version>
<features>
<feature>hourly10day</feature>
</features>
<hourly_forecast>
<forecast>
<FCTTIME>
<hour>16</hour>
<year>2012</year>
<mon_padded>10</mon_padded>
<mday_padded>05</mday_padded>
</FCTTIME>
<temp>
<english>102</english>
<metric>39</metric>
</temp>
</forecast>
</hourly_forecast>
</response>
Now, this works, but how could I apply the deserialization to it? Thanks!
Serialization is “easy” in the sense that you can convert XML into an object in a few lines of code. But depending on the complexity of the XML and/or object, there may be some setup you have to do (by way of adding attributes to the class definition) in order to have serialization be successful.
For example, with your XML a bit of decoration is warranted, I think. Here’s the beginnings of a set of classes which could represent your sample XML:
You will notice that many of the properties and a one of the classes are decorated with attributes. Theses attributes help the serializer coordinate the placement of each value within the XML into the appropriate property within the object. Ordinarily, one would name the classes and properties the same as what is found in XML. In doing so, you can often get away without having to use attributes. In this case, because .NET practice is to use Pascal casing for class and property names, and because your XML is using lowercase, I inserted the attributes to correctly associate each XML node with the corresponding property/class. Without doing so, the serializer would not be able to correctly map the elements to the object.
I mentioned that moving from the XML to the object in code could be accomplished in a few lines. Here is what that might look like:
While the code to construct the class which represents the XML admittedly does take a few lines, this is unavoidable. You would have something similar in your approach–less the attributes, of course. The savings here is that you convert the object in one swift call to the
Serializemethod.