0

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <name>test</name>
 <one>1</one>
 <two>2</two>
</test>

这是我的代码:

Imports System.Xml.Serialization
Imports System.IO

Class main

Sub Main()
    Dim p As New test()

    Dim x As New XmlSerializer(p.GetType)

    Dim objStreamReader As New StreamReader("XML.xml")
    Dim p2 As New class1()
    p2 = x.Deserialize(objStreamReader)
    objStreamReader.Close()

    MsgBox(p2.name)
    MsgBox(p2.one)
    MsgBox(p2.two)

End Sub

End Class

还有我的课:

Imports System.Xml.Serialization

Public Class test

Private newname As String
Private newone As Integer
Private newtwo As Integer

Public Property name() As String
    Get
        name = newname
    End Get
    Set(ByVal value As String)
        newname= value
    End Set
End Property

Public Property one() As Integer
    Get
        one = newone
    End Get
    Set(ByVal value As Integer)
        newone = value
    End Set
End Property

Public Property two() As Integer
    Get
        two = newtwo
    End Get
    Set(ByVal value As Integer)
        newtwo = value
    End Set
End Property

End Class

它有效,它为我提供了带有 XML 文件中数据的消息框,但是我遇到了麻烦,如果我像这样将内部节点添加到 XML 文件中:

<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <name>test</name>
 <numbers>
  <one>1</one>
  <two>2</two>
 </numbers>
</test>

我应该如何计算数字?我知道它是 test 的一个属性,但它也是一个类,因为它有一个和两个作为属性,那么正确的方法是什么?

更新:

<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <name>test</name>
 <numbers>
  <one>1</one>
  <two>2</two>
 </numbers>
 <numbers>
  <one>3</one>
  <two>4</two>
 </numbers>
</test>
4

1 回答 1

1

要反序列化该示例 XML,您希望数据类如下所示:

Public Class test
    Private newname As String
    Private newnumbers As List(Of Numbers)

    Public Property name() As String
        Get
            Return newname
        End Get
        Set(ByVal value As String)
            newname = value
        End Set
    End Property

    <XmlElement()> _
    Public Property numbers() As List(Of Numbers)
        Get
            Return newnumbers
        End Get
        Set(ByVal value As List(Of Numbers))
            newnumbers = value
        End Set
    End Property
End Class

Public Class Numbers
    Private newone As Integer
    Private newtwo As Integer

    Public Property one() As Integer
        Get
            Return newone
        End Get
        Set(ByVal value As Integer)
            newone = value
        End Set
    End Property

    Public Property two() As Integer
        Get
            Return newtwo
        End Get
        Set(ByVal value As Integer)
            newtwo = value
        End Set
    End Property
End Class

然后你可以像这样反序列化它:

Sub Main()
    Dim p As New test()
    Dim x As New XmlSerializer(p.GetType)
    Dim objStreamReader As New StreamReader("XML.xml")
    Dim p2 As New test()
    p2 = x.Deserialize(objStreamReader)
    objStreamReader.Close()
    MsgBox(p2.name)
    For Each i As Numbers In p2.numbers
        MsgBox(i.one)
        MsgBox(i.two)
    Next
End Sub
于 2012-10-08T17:54:01.477 回答