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



所以我有一个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应用程序中使用它们。

我试着在网上查找,但似乎没有简单的解决方案?任何帮助都将不胜感激!

如果您在模型上没有灵活性:

使用您的原始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

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

使用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模型,但这使得模型更加灵活。

最新更新