此XML模式是使用在线工具生成的,该工具从.xml提供.xsd模式
我有XML模式,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="snapshot">
<xs:complexType>
<xs:sequence>
<xs:element name="device" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="property" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="value" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="value" type="xs:string" />
<xs:attribute name="units" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="key" type="xs:string" />
<xs:attribute name="module" type="xs:string" />
<xs:attribute name="type" type="xs:string" />
<xs:attribute name="category" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="type" type="xs:string" />
<xs:attribute name="capabilities" type="xs:string" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="hwlocator" type="xs:string" />
<xs:attribute name="oslocator" type="xs:string" />
<xs:attribute name="ModelUID" type="xs:string" />
<xs:attribute name="DeviceUID" type="xs:string" />
<xs:attribute name="LocationUID" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="datetime" type="xs:string" />
<xs:attribute name="locale" type="xs:string" />
<xs:attribute name="version" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="snapshot" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
我要做的下一步是生成如下的vb类:
使用命令行:xsd.exe path.xsd /classes /language:vb /out:output
'
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
'
'This source code was auto-generated by xsd, Version=2.0.50727.3038.
'
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)> _
Partial Public Class snapshot
Private deviceField() As snapshotDevice
Private datetimeField As String
Private localeField As String
Private versionField As String
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("device", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property device() As snapshotDevice()
Get
Return Me.deviceField
End Get
Set
Me.deviceField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property datetime() As String
Get
Return Me.datetimeField
End Get
Set
Me.datetimeField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property locale() As String
Get
Return Me.localeField
End Get
Set
Me.localeField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property version() As String
Get
Return Me.versionField
End Get
Set
Me.versionField = value
End Set
End Property
End Class
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _
Partial Public Class snapshotDevice
Private propertyField() As snapshotDeviceProperty
Private typeField As String
Private capabilitiesField As String
Private nameField As String
Private hwlocatorField As String
Private oslocatorField As String
Private modelUIDField As String
Private deviceUIDField As String
Private locationUIDField As String
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("property", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property [property]() As snapshotDeviceProperty()
Get
Return Me.propertyField
End Get
Set
Me.propertyField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return Me.typeField
End Get
Set
Me.typeField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property capabilities() As String
Get
Return Me.capabilitiesField
End Get
Set
Me.capabilitiesField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property name() As String
Get
Return Me.nameField
End Get
Set
Me.nameField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property hwlocator() As String
Get
Return Me.hwlocatorField
End Get
Set
Me.hwlocatorField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property oslocator() As String
Get
Return Me.oslocatorField
End Get
Set
Me.oslocatorField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property ModelUID() As String
Get
Return Me.modelUIDField
End Get
Set
Me.modelUIDField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property DeviceUID() As String
Get
Return Me.deviceUIDField
End Get
Set
Me.deviceUIDField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property LocationUID() As String
Get
Return Me.locationUIDField
End Get
Set
Me.locationUIDField = value
End Set
End Property
End Class
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _
Partial Public Class snapshotDeviceProperty
Private valueField() As snapshotDevicePropertyValue
Private keyField As String
Private moduleField As String
Private typeField As String
Private categoryField As String
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("value", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property value() As snapshotDevicePropertyValue()
Get
Return Me.valueField
End Get
Set
Me.valueField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property key() As String
Get
Return Me.keyField
End Get
Set
Me.keyField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property [module]() As String
Get
Return Me.moduleField
End Get
Set
Me.moduleField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return Me.typeField
End Get
Set
Me.typeField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property category() As String
Get
Return Me.categoryField
End Get
Set
Me.categoryField = value
End Set
End Property
End Class
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _
Partial Public Class snapshotDevicePropertyValue
Private valueField As String
Private unitsField As String
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property value() As String
Get
Return Me.valueField
End Get
Set
Me.valueField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property units() As String
Get
Return Me.unitsField
End Get
Set
Me.unitsField = value
End Set
End Property
End Class
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)> _
Partial Public Class NewDataSet
Private itemsField() As snapshot
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("snapshot")> _
Public Property Items() As snapshot()
Get
Return Me.itemsField
End Get
Set
Me.itemsField = value
End Set
End Property
End Class
现在我如何反序列化该对象:使用这样的代码
Dim serializer As New XmlSerializer(GetType(List(Of snapshot)), New XmlRootAttribute("snapshotDevice"))
Dim deserialized As List(Of snapshot) = Nothing
Using file = System.IO.File.OpenRead(XMLFile)
deserialized = DirectCast(serializer.Deserialize(file), List(Of snapshot))
End Using
我的对象什么也不返回,即我想读取此选定值
答案 0 :(得分:0)
一种解决方案是使用DataSet.ReadXML
Dim dsPubs As New DataSet()
dsPubs.ReadXml("path.xml", XmlReadMode.Auto)
Dim dtKeys As DataTable = New DataView(dsPubs.Tables(2)).ToTable
Dim dtValues As DataTable = New DataView(dsPubs.Tables(3)).ToTable
Dim tbPairs As New DataTable
' Define columns
tbPairs.Columns.Add("ATTRIBUTE", GetType(System.String))
tbPairs.Columns.Add("ATTRIBUTE_DESC", GetType(System.String))
For I = 0 To dtKeys.Rows.Count - 1
tbPairs.Rows.Add(dtKeys.Rows(I).Item(1).ToString, dtValues.Rows(I).Item(0).ToString)
Next
DataGridView1.DataSource = tbPairs