【问题标题】:how to write XML from XSD using XMLWriter in VS如何在 VS 中使用 XMLWriter 从 XSD 写入 XML
【发布时间】:2015-09-09 04:17:57
【问题描述】:

以下问题:我想从我的数据库中创建一个 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>

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

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

【问题讨论】:

    标签: xml vb.net xsd xmlserializer xmlwriter


    【解决方案1】:

    你的解析可能是这样的

        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
    

    【讨论】:

    • 有趣的想法,用我的旧表创建一个新表。明天我会检查的。
    • 我只是展示了如何对数据表中的项目进行分组并从表中动态创建类。
    • 好的,谢谢。我刚刚遇到了同样的问题:现在我有了我的列表,想将一个类 Detail 的列表放入 Cylinder,例如 Cylinder.Detail = {l_Mydet(0), l_Mydet(1), l_Mydet(2)} 并且不能这样做。
    • 我仍然用最后一列覆盖所有条目。
    • 好的最后一条评论:如果我使用 detail 作为二维数组,我可以添加 MyresCy.Detail = {l_Mydet(0),l_Mydet(1)....}。我可以在观察程序中看到完整的 XML 树,它看起来不错,但 XML 序列化程序无法编译它。
    【解决方案2】:

    试试这个

    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
    

    【讨论】:

    • 感谢您的重播。虽然通常它可以工作并产生输出,但它存在一些问题,请参阅我的编辑以获取更多详细信息。
    • 尊敬的用户,我了解您的解决方法,但这不是我需要的。问题是,“Detail-elements”和“Cylinder-elements”的数量是未知的,需要动态分配。我基本上做的是:我浏览一些表格并将一些结果填充到圆柱体/详细信息分支中。我可以拥有多个包含多个详细信息的圆柱体,具体取决于它们内部的内容。所以我需要将所有 n 个细节添加到圆柱体,然后将所有 m 个圆柱体添加到一个 XML 文件中的一个位置
    • 您必须通过解析表在代码中动态构建类。我只是使用固定值作为样本。动态构建类后,代码将自动创建单个 XML 文件。需要表格样本以进一步帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多