您有与您的 xml 架构匹配的 vb.net 类吗?如果没有,你可以按照这个帖子生成一个https://stackoverflow.com/a/17315863/832052。
类应该是这样的:
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class Document
Private pointRecordField() As DocumentPointRecord
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("PointRecord")> _
Public Property PointRecord() As DocumentPointRecord()
Get
Return Me.pointRecordField
End Get
Set(value As DocumentPointRecord())
Me.pointRecordField = value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecord
Private nameField As String
Private codeField As String
Private methodField As String
Private classificationField As String
Private deletedField As Boolean
Private eCEFDeltasField As DocumentPointRecordECEFDeltas
Private idField As Byte
Private timeStampField As Date
'''<remarks/>
Public Property Name() As String
Get
Return Me.nameField
End Get
Set(value As String)
Me.nameField = value
End Set
End Property
'''<remarks/>
Public Property Code() As String
Get
Return Me.codeField
End Get
Set(value As String)
Me.codeField = value
End Set
End Property
'''<remarks/>
Public Property Method() As String
Get
Return Me.methodField
End Get
Set(value As String)
Me.methodField = value
End Set
End Property
'''<remarks/>
Public Property Classification() As String
Get
Return Me.classificationField
End Get
Set(value As String)
Me.classificationField = value
End Set
End Property
'''<remarks/>
Public Property Deleted() As Boolean
Get
Return Me.deletedField
End Get
Set(value As Boolean)
Me.deletedField = value
End Set
End Property
'''<remarks/>
Public Property ECEFDeltas() As DocumentPointRecordECEFDeltas
Get
Return Me.eCEFDeltasField
End Get
Set(value As DocumentPointRecordECEFDeltas)
Me.eCEFDeltasField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property ID() As Byte
Get
Return Me.idField
End Get
Set(value As Byte)
Me.idField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property TimeStamp() As Date
Get
Return Me.timeStampField
End Get
Set(value As Date)
Me.timeStampField = value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltas
Private deltaXField As DocumentPointRecordECEFDeltasDeltaX
Private deltaYField As DocumentPointRecordECEFDeltasDeltaY
Private deltaZField As DocumentPointRecordECEFDeltasDeltaZ
'''<remarks/>
Public Property DeltaX() As DocumentPointRecordECEFDeltasDeltaX
Get
Return Me.deltaXField
End Get
Set(value As DocumentPointRecordECEFDeltasDeltaX)
Me.deltaXField = value
End Set
End Property
'''<remarks/>
Public Property DeltaY() As DocumentPointRecordECEFDeltasDeltaY
Get
Return Me.deltaYField
End Get
Set(value As DocumentPointRecordECEFDeltasDeltaY)
Me.deltaYField = value
End Set
End Property
'''<remarks/>
Public Property DeltaZ() As DocumentPointRecordECEFDeltasDeltaZ
Get
Return Me.deltaZField
End Get
Set(value As DocumentPointRecordECEFDeltasDeltaZ)
Me.deltaZField = value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltasDeltaX
Private valueField As Decimal
Private inToleranceField As String
'''<remarks/>
Public Property Value() As Decimal
Get
Return Me.valueField
End Get
Set(value As Decimal)
Me.valueField = value
End Set
End Property
'''<remarks/>
Public Property InTolerance() As String
Get
Return Me.inToleranceField
End Get
Set(value As String)
Me.inToleranceField = value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltasDeltaY
Private valueField As Decimal
Private inToleranceField As String
'''<remarks/>
Public Property Value() As Decimal
Get
Return Me.valueField
End Get
Set(value As Decimal)
Me.valueField = value
End Set
End Property
'''<remarks/>
Public Property InTolerance() As String
Get
Return Me.inToleranceField
End Get
Set(value As String)
Me.inToleranceField = value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltasDeltaZ
Private valueField As Decimal
Private inToleranceField As String
'''<remarks/>
Public Property Value() As Decimal
Get
Return Me.valueField
End Get
Set(value As Decimal)
Me.valueField = value
End Set
End Property
'''<remarks/>
Public Property InTolerance() As String
Get
Return Me.inToleranceField
End Get
Set(value As String)
Me.inToleranceField = value
End Set
End Property
End Class
现在您在 vb.net 中有一个类,您可以将 xml 文档序列化和反序列化为该类的实例。
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Sub Main()
Dim s = New XmlSerializer(GetType(Document))
Dim d As Document
' use your filename here
Using sr = New StreamReader("document.xml")
d = CType(s.Deserialize(sr), Document)
End Using
For Each pr In d.PointRecord
Console.WriteLine("Point Record ID: {0} Time Stamp: {1}",
pr.ID, pr.TimeStamp)
Console.WriteLine(" Name: {0}",
pr.Name)
Console.WriteLine(" Code: {0}",
pr.Code)
Console.WriteLine(" Method: {0}",
pr.Method)
Console.WriteLine(" Classication: {0}",
pr.Classification)
Console.WriteLine(" Deleted: {0}",
pr.Deleted)
Console.WriteLine(" ECFDeltas-")
Console.WriteLine(" DeltaX: {0:0.000000000} In Tolerance: {1}",
pr.ECEFDeltas.DeltaX.Value, pr.ECEFDeltas.DeltaX.InTolerance)
Console.WriteLine(" DeltaY: {0:0.000000000} In Tolerance: {1}",
pr.ECEFDeltas.DeltaY.Value, pr.ECEFDeltas.DeltaY.InTolerance)
Console.WriteLine(" DeltaZ: {0:0.000000000} In Tolerance: {1}",
pr.ECEFDeltas.DeltaZ.Value, pr.ECEFDeltas.DeltaZ.InTolerance)
Next
Console.ReadLine()
End Sub
我添加了另一个<PointRecord ID="00000050" TimeStamp="2017-03-03T09:39:54"> 节点来生成具有PointRecord 数组的模型,假设你有这个。
<Document>
<PointRecord ID="00000050" TimeStamp="2017-03-03T09:39:54">
<Name>WF2510</Name>
<Code>EOC RECT 6/</Code>
<Method>StaticObservation</Method>
<Classification>Normal</Classification>
<Deleted>false</Deleted>
<ECEFDeltas>
<DeltaX>
<Value>-14179.040909261</Value>
<InTolerance>True</InTolerance>
</DeltaX>
<DeltaY>
<Value>-3572.6636230592</Value>
<InTolerance>True</InTolerance>
</DeltaY>
<DeltaZ>
<Value>-8319.8607607852</Value>
<InTolerance>False</InTolerance>
</DeltaZ>
</ECEFDeltas>
</PointRecord>
<PointRecord ID="00000051" TimeStamp="2017-03-03T09:39:54">
<Name>WF2510</Name>
<Code>EOC RECT 6/</Code>
<Method>StaticObservation</Method>
<Classification>Normal</Classification>
<Deleted>false</Deleted>
<ECEFDeltas>
<DeltaX>
<Value>-14179.040909261</Value>
<InTolerance>True</InTolerance>
</DeltaX>
<DeltaY>
<Value>-3572.6636230592</Value>
<InTolerance>True</InTolerance>
</DeltaY>
<DeltaZ>
<Value>-8319.8607607852</Value>
<InTolerance>False</InTolerance>
</DeltaZ>
</ECEFDeltas>
</PointRecord>
</Document>
测试时,我的 xml 文档有两个 PointRecords。程序的输出:
Point Record ID: 50 Time Stamp: 3/3/2017 9:39:54 AM
Name: WF2510
Code: EOC RECT 6/
Method: StaticObservation
Classication: Normal
Deleted: False
ECFDeltas-
DeltaX: -14179.040909261 In Tolerance: True
DeltaY: -3572.663623059 In Tolerance: True
DeltaZ: -8319.860760785 In Tolerance: False
Point Record ID: 51 Time Stamp: 3/3/2017 9:39:54 AM
Name: WF2510
Code: EOC RECT 6/
Method: StaticObservation
Classication: Normal
Deleted: False
ECFDeltas-
DeltaX: -14179.040909261 In Tolerance: True
DeltaY: -3572.663623059 In Tolerance: True
DeltaZ: -8319.860760785 In Tolerance: False