读取XML并存储为VB变量的最基本方法

时间:2013-12-11 16:57:53

标签: html xml vb.net

所以我有一个看起来像这样的XML文件:

<response>
<led0>0</led0> 
<led1>1</led1> 
<pot0>0.0</pot0> 
<pot1>0.0</pot1> 
<temp1>22.2</temp1> 
<temp2>***</temp2> 
<temp3>***</temp3> 
</response>

我所要做的就是将这些行中的每一行作为VB中的变量存储在XML中。

例如:XML中的第3行= VB中的Dim LED1。

这样它们就可以更容易地在Web应用程序中使用。

我试过在网上看,但似乎没有一个简单的解决方案吗?任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

如果您对模型没有灵活性:

使用原始xml文件

Private Sub testXmlReadingAndWriting()
    Try
        Dim filename As String = "<your file name here>"
        Dim myResponse As Response = Nothing
        Dim serializer As New XmlSerializer(GetType(Response))
        Using sr As New StreamReader(filename)
            myResponse = CType(serializer.Deserialize(sr), Response)
        End Using
        myResponse.led0 = 0.7
        myResponse.temp3 = 23.ToString()
        Using sw As New StreamWriter(filename)
            serializer.Serialize(sw, myResponse)
        End Using
    Catch ex As Exception
        MessageBox.Show("Exception handled: " & ex.Message)
    End Try
End Sub

<XmlRoot("response")> _
Public Class Response
    <XmlElement()> _
    Public led0 As Single
    <XmlElement()> _
    Public led1 As Single
    <XmlElement()> _
    Public pot0 As Single
    <XmlElement()> _
    Public pot1 As Single
    <XmlElement()> _
    Public temp1 As String
    <XmlElement()> _
    Public temp2 As String
    <XmlElement()> _
    Public temp3 As String
    Public Sub New()
    End Sub
End Class

答案 1 :(得分:0)

如果你对模型有灵活性,这个答案是:

使用Xml序列化,您可以在内存中有一个强类型的xml表示形式(变量,如您所要求的那样)。

考虑这个XML:

<response>
  <measurement Type="Led" Index="0" Value="0" />
  <measurement Type="Led" Index="1" Value="1" />
  <measurement Type="Pot" Index="0" Value="0" />
  <measurement Type="Pot" Index="1" Value="0" />
  <measurement Type="Temp" Index="1" Value="22.2" />
  <measurement Type="Temp" Index="2" Value="0" />
  <measurement Type="Temp" Index="3" Value="0" />
</response>

使用xml模型类,可以从xml序列化和反序列化为变量。这是从xml读取的代码,修改值,并写入xml:

Private Sub testXmlReadingAndWriting()
    Try
        Dim filename As String = "<your file name here>"
        Dim myResponse As Response = Nothing
        Dim serializer As New XmlSerializer(GetType(Response))
        Using sr As New StreamReader(filename)
            myResponse = CType(serializer.Deserialize(sr), Response)
        End Using
        myResponse.Measurements.Add(New Measurement(2, 0.7, MeasurementType.Led))
        myResponse.Measurements.Add(New Measurement(2, 10700, MeasurementType.Pot))
        myResponse.Measurements.Add(New Measurement(4, 23, MeasurementType.Temp))
        Using sw As New StreamWriter(filename)
            serializer.Serialize(sw, myResponse)
        End Using
    Catch ex As Exception
        MessageBox.Show("Exception handled: " & ex.Message)
    End Try
End Sub

<XmlRoot("response")> _
Public Class Response
    <XmlElement("measurement")> _
    Public Measurements As List(Of Measurement)
    Public Sub New()
    End Sub
End Class

Public Class Measurement
    <XmlAttribute> _
    Public [Type] As MeasurementType
    <XmlAttribute> _
    Public Index As Integer
    <XmlAttribute> _
    Public Value As Single
    Public Sub New(index As Integer, value As Single, [type] As MeasurementType)
        Me.Index = index
        Me.Value = value
        Me.[Type] = [type]
    End Sub
    Public Sub New()
    End Sub
End Class

Public Enum MeasurementType
    Led
    Pot
    Temp
End Enum

我意识到我修改了你的xml模型,但这使得模型更加灵活。