如何在VS

时间:2015-06-23 11:28:33

标签: xml vb.net xsd xmlserializer xmlwriter

以下问题:我想从我的数据库创建一个XML文档,该文档应具有特定的布局。我创建了一个XSD文件,它代表了XML的结构:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="urn:com.xx.xx" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:com.xx.xx">
<xsd:element name="MT_AnalyseQueueResponse_EXT" type="DT_AnalyseQueue_EXT"/>
  <xsd:complexType name="DT_AnalyseQueue_EXT">
  <xsd:annotation>
<xsd:documentation xml:lang="EN">Data Type for Analyse Queue Response</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
  <xsd:element name="Res">
  <xsd:complexType>
  <xsd:sequence>
    <xsd:element name="Location" maxOccurs="unbounded">
    <xsd:complexType>
    <xsd:attribute name="No" type="xsd:string"/>
    <xsd:attribute name="Testpoint" type="xsd:string"/>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="Results" maxOccurs="unbounded">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="Cylinder" maxOccurs="unbounded">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="Detail" maxOccurs="unbounded">
    <xsd:complexType>
    <xsd:attribute name="Component" type="xsd:string"/>
    <xsd:attribute name="DeviceID" type="xsd:string"/>
    <xsd:attribute name="Value" type="xsd:string"/>
    <xsd:attribute name="Info" type="xsd:string"/>
    <xsd:attribute name="Result" type="xsd:string"/>
    <xsd:attribute name="Unit" type="xsd:string"/>
    <xsd:attribute name="Limit" type="xsd:string"/>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
      <xsd:attribute name="Date" type="xsd:string"/>
      <xsd:attribute name="BatchNo" type="xsd:string"/>
      <xsd:attribute name="Result" type="xsd:string"/>
      <xsd:attribute name="Info" type="xsd:string"/>
      <xsd:attribute name="BID" type="xsd:string"/>
      <xsd:attribute name="Complete" type="xsd:string"/>
      <xsd:attribute name="Release" type="xsd:string"/>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>   

使用这个XSD,Visual Studio向我展示了一个从XSD创建的XML,看起来很不错:

<?xml version="1.0" encoding="utf-8"?>
<MT_AnalyseQueueResponse_EXT xmlns="urn:com.xx.xx">
  <Res xmlns="">
    <Location No="No1" Testpoint="Testpoint1" />
    <Results>
      <Cylinder Date="Date1" BatchNo="BatchNo1" Result="Result1" Info="Info1" BID="BID1" Complete="Complete1" Release="Release1">
        <Detail Component="Component1" DeviceID="DeviceID1" Value="Value1" Info="Info1" Result="Result1" Unit="Unit1" Limit="Limit1" />
      </Cylinder>
     </Results>
  </Res>
</MT_AnalyseQueueResponse_EXT>

现在我的问题是,我不确定如何创建XML。我使用XSD创建.VB文件,其中包含XSD中的类。 使用这些,我想填充不同的元素和属性,并使用序列化程序和XML编写器来创建XML文件,但它不起作用,我只是得到这个输出:

<?xml version="1.0" encoding="iso-8859-1"?>
<DT_AnalyseQueue_EXTResLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" No="2000" Testpoint="MED12" />

我猜问题是隐藏在我使用不同类的部分中的某个地方,并尝试将它们添加到一起。这是我的代码:

Dim MyCylResultsLoc As DT_AnalyseQueue_EXTResLocation = New DT_AnalyseQueue_EXTResLocation() _
                                                              With {.No = sLocNo, .Testpoint = drRes("TP")}     
Dim MyCylResultsResCylDet As DT_AnalyseQueue_EXTResResultsCylinderDetail = New DT_AnalyseQueue_EXTResResultsCylinderDetail() _
                                                                                  With {.Component = drRes("AA"),
                                                                                        .DeviceID = drRes("BB"),
                                                                                        .Result = drRes("CC"), 
                                                                                        .Value = drRes("VV"),          
                                              .Unit = drRes("UU")}
          '    'write XML export file with ISO-8859-1 encoding and \r\n line feed
          Dim MyCylResultsResCyl As DT_AnalyseQueue_EXTResResultsCylinder = New DT_AnalyseQueue_EXTResResultsCylinder() _
                                                                            With {.BatchNo = drRes("Ba"),
                                                                                  .BID = drRes("B"),
                                                                                  .Complete = drRes("CC"), 
                                                                                  .Date = sPruevDatuB,
                                                                                  .Result = drRes("RR"),
                                                                                  .Detail = ?????} ' here I would use the class name of the detail branch but that is not working

             '28591
          Dim MyXMLWriterSettings As Xml.XmlWriterSettings = New Xml.XmlWriterSettings _
          With {.Encoding = System.Text.Encoding.GetEncoding(28591), .Indent = True, .IndentChars = vbTab, .NewLineChars = vbCrLf}

          Dim MyXMLWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sExportPath & sExportFileName & ".XML", MyXMLWriterSettings)

          Dim MyXMLSerializerLoc As New XmlSerializer(GetType(DT_AnalyseQueue_EXTResLocation))
          MyXMLSerializerLoc.Serialize(MyXMLWriter, MyCylResultsLoc)

          Dim MyXMLSerializerCyl As New XmlSerializer(GetType(DT_AnalyseQueue_EXTResResultsCylinder))
          MyXMLSerializerCyl.Serialize(MyXMLWriter, MyCylResultsResCyl)

          Dim MyXMLSerializercylDet As New XmlSerializer(GetType(DT_AnalyseQueue_EXTResResultsCylinderDetail))
          MyXMLSerializercylDet.Serialize(MyXMLWriter, MyCylResultsResCylDet)
          MyXMLWriter.Close()

任何可以推动我朝着正确方向前进的人? 编辑:更多信息

好的,虽然我得到了一个有效的例子,但我并不满足于此。 首先,在一个XML中,我应该有几个同名的子元素,就像我的XSD文件生成的示例XML一样:

<?xml version="1.0" encoding="utf-8"?>
<MT_AnalyseQueueResponse_EXT xmlns="urn:com.xxx.xxx">
  <Res xmlns="">
    <Location No="No1" Testpoint="Testpoint1" />
    <Results>
      <Cylinder Date="Date1" BatchNo="BatchNo1" Result="Result1" Info="Info1" BID="BID1" Complete="Complete1" Release="Release1">
        <Detail Component="Component1" DeviceID="DeviceID1" Value="Value1" Info="Info1" Result="Result1" Unit="Unit1" Limit="Limit1" />
       </Cylinder>
      <Cylinder Date="Date2" BatchNo="BatchNo2" Result="Result2" Info="Info2" BID="BID2" Complete="Complete2" Release="Release2">
        <Detail Component="Component4" DeviceID="DeviceID4" Value="Value4" Info="Info4" Result="Result4" Unit="Unit4" Limit="Limit4" />
        <Detail Component="Component5" DeviceID="DeviceID5" Value="Value5" Info="Info5" Result="Result5" Unit="Unit5" Limit="Limit5" />
       </Cylinder>
      <Cylinder Date="Date3" BatchNo="BatchNo3" Result="Result3" Info="Info3" BID="BID3" Complete="Complete3" Release="Release3">
        <Detail Component="Component7" DeviceID="DeviceID7" Value="Value7" Info="Info7" Result="Result7" Unit="Unit7" Limit="Limit7" />
        <Detail Component="Component8" DeviceID="DeviceID8" Value="Value8" Info="Info8" Result="Result8" Unit="Unit8" Limit="Limit8" />
      </Cylinder>
    </Results>
    <Results>
      <Cylinder Date="Date1" BatchNo="BatchNo1" Result="Result1" Info="Info1" BID="BID1" Complete="Complete1" Release="Release1">
        <Detail Component="Component1" DeviceID="DeviceID1" Value="Value1" Info="Info1" Result="Result1" Unit="Unit1" Limit="Limit1" />
      </Cylinder>
     </Results>
  </Res>
</MT_AnalyseQueueResponse_EXT>

子结构的数量可能会有所不同。这就是为什么我认为我可以使用我的XSD编译的* .vb文件:

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

'
'This source code was auto-generated by xsd, Version=4.0.30319.1.
'

'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:com.xx.xx"),  _
 System.Xml.Serialization.XmlRootAttribute("MT_AnalyseQueueResponse_EXT", [Namespace]:="urn:com.xxx.xx", IsNullable:=false)>  _
Partial Public Class DT_AnalyseQueue_EXT

    Private resField As DT_AnalyseQueue_EXTRes

    '<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property Res() As DT_AnalyseQueue_EXTRes
        Get
            Return Me.resField
        End Get
        Set
            Me.resField = value
        End Set
    End Property
End Class

'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xxx.xx")>  _
Partial Public Class DT_AnalyseQueue_EXTRes

    Private locationField() As DT_AnalyseQueue_EXTResLocation

    Private resultsField()() As DT_AnalyseQueue_EXTResResultsCylinder

  Sub New()
    ' TODO: Complete member initialization 
  End Sub

    '<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("Location", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property Location() As DT_AnalyseQueue_EXTResLocation()
        Get
            Return Me.locationField
        End Get
        Set
            Me.locationField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified),  _
     System.Xml.Serialization.XmlArrayItemAttribute("Cylinder", GetType(DT_AnalyseQueue_EXTResResultsCylinder), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=false)>  _
    Public Property Results() As DT_AnalyseQueue_EXTResResultsCylinder()()
        Get
            Return Me.resultsField
        End Get
        Set
            Me.resultsField = value
        End Set
    End Property
End Class

'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xx.xx")>  _
Partial Public Class DT_AnalyseQueue_EXTResLocation

    Private noField As String

    Private testpointField As String

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property No() As String
        Get
            Return Me.noField
        End Get
        Set
            Me.noField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Testpoint() As String
        Get
            Return Me.testpointField
        End Get
        Set
            Me.testpointField = value
        End Set
    End Property
End Class

'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xxx.xx")>  _
Partial Public Class DT_AnalyseQueue_EXTResResultsCylinder

    Private detailField() As DT_AnalyseQueue_EXTResResultsCylinderDetail

    Private dateField As String

    Private batchNoField As String

    Private resultField As String

    Private infoField As String

    Private bIDField As String

    Private completeField As String

    Private releaseField As String

    '<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("Detail", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property Detail() As DT_AnalyseQueue_EXTResResultsCylinderDetail()
        Get
            Return Me.detailField
        End Get
        Set
            Me.detailField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property [Date]() As String
        Get
            Return Me.dateField
        End Get
        Set
            Me.dateField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property BatchNo() As String
        Get
            Return Me.batchNoField
        End Get
        Set
            Me.batchNoField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Result() As String
        Get
            Return Me.resultField
        End Get
        Set
            Me.resultField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Info() As String
        Get
            Return Me.infoField
        End Get
        Set
            Me.infoField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property BID() As String
        Get
            Return Me.bIDField
        End Get
        Set
            Me.bIDField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Complete() As String
        Get
            Return Me.completeField
        End Get
        Set
            Me.completeField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Release() As String
        Get
            Return Me.releaseField
        End Get
        Set
            Me.releaseField = value
        End Set
    End Property
End Class

'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xxx.xxx")>  _
Partial Public Class DT_AnalyseQueue_EXTResResultsCylinderDetail

    Private componentField As String

    Private deviceIDField As String

    Private valueField As String

    Private infoField As String

    Private resultField As String

    Private unitField As String

    Private limitField As String

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Component() As String
        Get
            Return Me.componentField
        End Get
        Set
            Me.componentField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property DeviceID() As String
        Get
            Return Me.deviceIDField
        End Get
        Set
            Me.deviceIDField = value
        End Set
    End Property

    '<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 Info() As String
        Get
            Return Me.infoField
        End Get
        Set
            Me.infoField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Result() As String
        Get
            Return Me.resultField
        End Get
        Set
            Me.resultField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Unit() As String
        Get
            Return Me.unitField
        End Get
        Set
            Me.unitField = value
        End Set
    End Property

    '<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()>  _
    Public Property Limit() As String
        Get
            Return Me.limitField
        End Get
        Set
            Me.limitField = value
        End Set
    End Property
End Class

2 个答案:

答案 0 :(得分:0)

试试这个

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim mT_AnalyseQueueResponse_EXT As MT_AnalyseQueueResponse_EXT = New MT_AnalyseQueueResponse_EXT() With { _
            .mRes = New Res() With { _
                .mLocation = New Location() With {.No = "No1", .Testpoint = "Testpoint1"}, _
                .mResults = New List(Of Results)(New Results() { _
                    New Results With { _
                        .mCylinder = New List(Of Cylinder)(New Cylinder() { _
                            New Cylinder() With { _
                               .mDate = DateTime.Today(), _
                               .BatchNo = "BatchNo1", _
                               .Result = "Result1", _
                               .Info = "Info1", _
                               .BID = "BID1", _
                               .Complete = True, _
                               .Release = "Release1", _
                               .Detail = New List(Of Detail)(New Detail() { _
                                  New Detail() With { _
                                     .Component = "Component1", _
                                     .DeviceID = "DeviceID1", _
                                     .Value = "Value1", _
                                     .Info = "Info1", _
                                     .Result = "Result1", _
                                     .Unit = "Unit1", _
                                     .Limit = "Limit1" _
                                  }, _
                                  New Detail() With { _
                                     .Component = "Component2", _
                                     .DeviceID = "DeviceID2", _
                                     .Value = "Value2", _
                                     .Info = "Info2", _
                                     .Result = "Result2", _
                                     .Unit = "Unit2", _
                                     .Limit = "Limit1" _
                                  } _
                               }) _
                           }, _
                           New Cylinder() With { _
                               .mDate = DateTime.Today(), _
                               .BatchNo = "BatchNo2", _
                               .Result = "Result2", _
                               .Info = "Info2", _
                               .BID = "BID2", _
                               .Complete = True, _
                               .Release = "Release2", _
                               .Detail = New List(Of Detail)(New Detail() { _
                                  New Detail() With { _
                                     .Component = "Component1", _
                                     .DeviceID = "DeviceID1", _
                                     .Value = "Value1", _
                                     .Info = "Info1", _
                                     .Result = "Result1", _
                                     .Unit = "Unit1", _
                                     .Limit = "Limit1" _
                                  } _
                               }) _
                           }, _
                           New Cylinder() With { _
                               .mDate = DateTime.Today(), _
                               .BatchNo = "BatchNo3", _
                               .Result = "Result3", _
                               .Info = "Info3", _
                               .BID = "BID3", _
                               .Complete = True, _
                               .Release = "Release2", _
                               .Detail = New List(Of Detail)(New Detail() { _
                                  New Detail() With { _
                                     .Component = "Component1", _
                                     .DeviceID = "DeviceID1", _
                                     .Value = "Value1", _
                                     .Info = "Info1", _
                                     .Result = "Result1", _
                                     .Unit = "Unit1", _
                                     .Limit = "Limit1" _
                                  } _
                               }) _
                           } _
                        }) _
                    }, _
                                        New Results With { _
                        .mCylinder = New List(Of Cylinder)(New Cylinder() { _
                            New Cylinder() With { _
                               .mDate = DateTime.Today(), _
                               .BatchNo = "BatchNo1", _
                               .Result = "Result1", _
                               .Info = "Info1", _
                               .BID = "BID1", _
                               .Complete = True, _
                               .Release = "Release1", _
                               .Detail = New List(Of Detail)(New Detail() { _
                                  New Detail() With { _
                                     .Component = "Component1", _
                                     .DeviceID = "DeviceID1", _
                                     .Value = "Value1", _
                                     .Info = "Info1", _
                                     .Result = "Result1", _
                                     .Unit = "Unit1", _
                                     .Limit = "Limit1" _
                                  }, _
                                  New Detail() With { _
                                     .Component = "Component2", _
                                     .DeviceID = "DeviceID2", _
                                     .Value = "Value2", _
                                     .Info = "Info2", _
                                     .Result = "Result2", _
                                     .Unit = "Unit2", _
                                     .Limit = "Limit1" _
                                  } _
                               }) _
                           }, _
                           New Cylinder() With { _
                               .mDate = DateTime.Today(), _
                               .BatchNo = "BatchNo2", _
                               .Result = "Result2", _
                               .Info = "Info2", _
                               .BID = "BID2", _
                               .Complete = True, _
                               .Release = "Release2", _
                               .Detail = New List(Of Detail)(New Detail() { _
                                  New Detail() With { _
                                     .Component = "Component1", _
                                     .DeviceID = "DeviceID1", _
                                     .Value = "Value1", _
                                     .Info = "Info1", _
                                     .Result = "Result1", _
                                     .Unit = "Unit1", _
                                     .Limit = "Limit1" _
                                  } _
                               }) _
                           }, _
                           New Cylinder() With { _
                               .mDate = DateTime.Today(), _
                               .BatchNo = "BatchNo3", _
                               .Result = "Result3", _
                               .Info = "Info3", _
                               .BID = "BID3", _
                               .Complete = True, _
                               .Release = "Release2", _
                               .Detail = New List(Of Detail)(New Detail() { _
                                  New Detail() With { _
                                     .Component = "Component1", _
                                     .DeviceID = "DeviceID1", _
                                     .Value = "Value1", _
                                     .Info = "Info1", _
                                     .Result = "Result1", _
                                     .Unit = "Unit1", _
                                     .Limit = "Limit1" _
                                  } _
                               }) _
                           } _
                        }) _
                      } _
                }) _
            } _
        }

        Dim serializer As XmlSerializer = New XmlSerializer(GetType(MT_AnalyseQueueResponse_EXT))

        Dim writer As StreamWriter = New StreamWriter(FILENAME)
        serializer.Serialize(writer, mT_AnalyseQueueResponse_EXT)
        writer.Flush()
        writer.Close()
        writer.Dispose()


        Dim xs As XmlSerializer = New XmlSerializer(GetType(MT_AnalyseQueueResponse_EXT))
        Dim reader As XmlTextReader = New XmlTextReader(FILENAME)
        Dim newMT As MT_AnalyseQueueResponse_EXT = CType(xs.Deserialize(reader), MT_AnalyseQueueResponse_EXT)


    End Sub

End Module

<XmlRoot("MT_AnalyseQueueResponse_EXT")> _
Public Class MT_AnalyseQueueResponse_EXT
    Private _rs As Res
    <XmlElement("Res")> _
    Property mRes() As Res
        Get
            Return _rs
        End Get
        Set(ByVal value As Res)
            _rs = value
        End Set
    End Property
End Class
<XmlRoot("Res")> _
Public Class Res
    Private _location As Location
    <XmlElement("Location")> _
    Property mLocation() As Location
        Get
            Return _location
        End Get
        Set(ByVal value As Location)
            _location = value
        End Set
    End Property

    Private _results As List(Of Results)
    <XmlElement("Results")> _
    Property mResults() As List(Of Results)
        Get
            Return _results
        End Get
        Set(ByVal value As List(Of Results))
            _results = value
        End Set
    End Property

End Class
<XmlRoot("Location")> _
Public Class Location
    Private _no As String
    <XmlAttribute("No")> _
    Property No() As String
        Get
            Return _no
        End Get
        Set(ByVal value As String)
            _no = value
        End Set
    End Property

    Private _testpoint As String
    <XmlAttribute("Testpoint")> _
    Property Testpoint() As String
        Get
            Return _testpoint
        End Get
        Set(ByVal value As String)
            _testpoint = value
        End Set
    End Property

End Class
<XmlRoot("Results")> _
Public Class Results
    Private _cylinder As List(Of Cylinder)
    <XmlElement("Cylinder")> _
    Property mCylinder() As List(Of Cylinder)
        Get
            Return _cylinder
        End Get
        Set(ByVal value As List(Of Cylinder))
            _cylinder = value
        End Set
    End Property

End Class
<XmlRoot("Cylinder")> _
Public Class Cylinder
    Private _detail As List(Of Detail)
    <XmlElement("Detail")> _
    Property Detail() As List(Of Detail)
        Get
            Return _detail
        End Get
        Set(ByVal value As List(Of Detail))
            _detail = value
        End Set
    End Property

    Private _date As DateTime
    <XmlAttribute("Date")> _
    Property mDate() As DateTime
        Get
            Return _date
        End Get
        Set(ByVal value As DateTime)
            _date = value
        End Set
    End Property

    Private _batchNo As String
    <XmlAttribute("BatchNo")> _
    Property BatchNo() As String
        Get
            Return _batchNo
        End Get
        Set(ByVal value As String)
            _batchNo = value
        End Set
    End Property

    Private _result As String
    <XmlAttribute("Result")> _
    Property Result() As String
        Get
            Return _result
        End Get
        Set(ByVal value As String)
            _result = value
        End Set
    End Property

    Private _info As String
    <XmlAttribute("Info")> _
    Property Info() As String
        Get
            Return _info
        End Get
        Set(ByVal value As String)
            _info = value
        End Set
    End Property

    Private _bID As String
    <XmlAttribute("BID")> _
    Property BID() As String
        Get
            Return _bID
        End Get
        Set(ByVal value As String)
            _bID = value
        End Set
    End Property

    Private _complete As Boolean
    <XmlAttribute("Complete")> _
    Property Complete() As Boolean
        Get
            Return _complete
        End Get
        Set(ByVal value As Boolean)
            _complete = value
        End Set
    End Property

    Private _release As String
    <XmlAttribute("Release")> _
    Property Release() As String
        Get
            Return _release
        End Get
        Set(ByVal value As String)
            _release = value
        End Set
    End Property

End Class
<XmlRoot("Detail")> _
Public Class Detail
    Private _component As String
    <XmlAttribute("Component")> _
    Property Component() As String
        Get
            Return _component
        End Get
        Set(ByVal value As String)
            _component = value
        End Set
    End Property

    Private _deviceID As String
    <XmlAttribute("DeviceID")> _
    Property DeviceID() As String
        Get
            Return _deviceID
        End Get
        Set(ByVal value As String)
            _deviceID = value
        End Set
    End Property

    Private _value As String
    <XmlAttribute("Value")> _
    Property Value() As String
        Get
            Return _value
        End Get
        Set(ByVal value As String)
            _value = value
        End Set
    End Property

    Private _info As String
    <XmlAttribute("Info")> _
    Property Info() As String
        Get
            Return _info
        End Get
        Set(ByVal value As String)
            _info = value
        End Set
    End Property

    Private _result As String
    <XmlAttribute("Result")> _
    Property Result() As String
        Get
            Return _result
        End Get
        Set(ByVal value As String)
            _result = value
        End Set
    End Property

    Private _unit As String
    <XmlAttribute("Unit")> _
    Property Unit() As String
        Get
            Return _unit
        End Get
        Set(ByVal value As String)
            _unit = value
        End Set
    End Property

    Private _limit As String
    <XmlAttribute("Limit")> _
    Property Limit() As String
        Get
            Return _limit
        End Get
        Set(ByVal value As String)
            _limit = value
        End Set
    End Property


End Class

答案 1 :(得分:0)

你解析可能看起来像这样

    Sub Main()

        Dim dt As New DataTable
        dt.Columns.Add("Results", GetType(String))
        dt.Columns.Add("Batch", GetType(String))
        dt.Columns.Add("Component", GetType(String))
        dt.Columns.Add("DeviceID", GetType(String))

        dt.Rows.Add(New String() {1, 10, 100, 1000})
        dt.Rows.Add(New String() {1, 10, 200, 1001})
        dt.Rows.Add(New String() {1, 10, 200, 1002})
        dt.Rows.Add(New String() {1, 10, 300, 1003})
        dt.Rows.Add(New String() {2, 20, 100, 1004})
        dt.Rows.Add(New String() {2, 20, 100, 1005})
        dt.Rows.Add(New String() {2, 20, 100, 1006})
        dt.Rows.Add(New String() {2, 30, 100, 1007})
        dt.Rows.Add(New String() {2, 30, 100, 1008})
        dt.Rows.Add(New String() {2, 30, 100, 1009})



        Dim mT_AnalyseQueueResponse_EXT As New MT_AnalyseQueueResponse_EXT

        Dim resultRows = dt.AsEnumerable() _
                         .GroupBy(Function(x) x.Field(Of String)("Results"))

        Dim newRes As New Res
        mT_AnalyseQueueResponse_EXT.mRes = newRes

        Dim l_Results As New List(Of Results)
        newRes.mResults = l_Results
        For Each result In resultRows
            Dim newResult As New Results
            l_Results.Add(newResult)
            Dim resultNo As String = result(0).Field(Of String)("Results")

            Dim batchRows = result.AsEnumerable() _
                             .GroupBy(Function(x) x.Field(Of String)("Batch"))

            Dim l_Cylinder As New List(Of Cylinder)
            newResult.mCylinder = l_Cylinder
            For Each batchRow In batchRows
                Dim newCylinder As New Cylinder
                l_Cylinder.Add(newCylinder)

                Dim batchNo As String = batchRow(0).Field(Of String)("Batch")
                Dim componentRows = batchRow.AsEnumerable() _
                  .GroupBy(Function(x) x.Field(Of String)("Component"))

                Dim l_Detail As New List(Of Detail)
                newCylinder.Detail = l_Detail
                For Each componentRow In componentRows
                    Dim newDetail As New Detail
                    l_Detail.Add(newDetail)

                    Dim datailNo As String = componentRow(0).Field(Of String)("DeviceID")
                Next

            Next batchRow

        Next result