我正在做一个项目,我必须将从 XML 文件读取的数据保存到数据库中,我已经在使用我制作的代码,但它有点原始,我正在使用 XMLTextReader 和其他一些方法来导航通过文件上的代码行并读取我想要的数据,但是如果我在我的程序上使用序列化,我已经读过这将是非常“容易”,现在我已经看到了一些这样的例子,老实说还不太了解它们,甚至以前从未听说过它们,所以如果有人可以笼统地向我解释它并提供一个小例子来说明我如何将它应用到我的项目中,那就太好了。
这就是我现在所拥有的:
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
这是我的 XML 的示例:
<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>
现在,这可行,但是我如何将反序列化应用于它?谢谢!