XML类看起来如何?

时间:2010-02-17 15:33:31

标签: c# asp.net xml vb.net xml-serialization

我需要知道序列化的XML类应该如何:

我需要这个XML

<?xml version="1.0" encoding="UTF-8"?>
<import date="2010-02-12T23:33:39">
  <T_Employee>
    <MI_KZ>HKBZV</MI_KZ>
    <MI_Name>John</MI_Name>
    <MI_Vorname>Doe</MI_Vorname>
    <MI_Nummer>987654321</MI_Nummer>
    <MI_DatumVon>2010-02-11T10:45:37</MI_DatumVon>
        <MI_DatumBis>2010-03-13T00:00:00</MI_DatumBis>
    <AP_Bezeichnung>5-B-03</AP_Bezeichnung>
    <KOE_Code>FHBM</KOE_Code>
    <KST_Code></KST_Code>
    <KST_Kurz><![CDATA[]]></KST_Kurz>
      </T_Employee>
  <T_Employee>
    <MI_KZ>EX2FC</MI_KZ>
    <MI_Name>Doe</MI_Name>
    <MI_Vorname>Johnny</MI_Vorname>
    <MI_Nummer>123456789</MI_Nummer>
    <MI_DatumVon>2010-01-13T05:52:16</MI_DatumVon>
    <MI_DatumBis>2010-02-01T23:00:00</MI_DatumBis>
    <AP_Bezeichnung>Test1</AP_Bezeichnung>
    <KOE_Code>Test2</KOE_Code>
    <KST_Code>Test3</KST_Code>
    <KST_Kurz>Test4</KST_Kurz>
  </T_Employee>

</import>

但到目前为止,我得到了

<?xml version="1.0" encoding="utf-8"?>
<import date="2010-02-12T23:33:39">
  <T_Employee>
    <cEmployee>
      <MI_KZ />
      <MI_Name>Doe</MI_Name>
          <MI_Vorname>John</MI_Vorname>
      <MI_Nummer />
      <MI_DatumVon />
      <MI_DatumBis />
      <AP_Bezeichnung />
      <KOE_Code>abc</KOE_Code>
      <KST_Code />
      <KST_Kurz />
    </cEmployee>
    <cEmployee>
      <MI_KZ />
      <MI_Name>Doe</MI_Name>
      <MI_Vorname>Johnny</MI_Vorname>
      <MI_Nummer />
      <MI_DatumVon />
      <MI_DatumBis />
      <AP_Bezeichnung />
      <KOE_Code>def</KOE_Code>
      <KST_Code />
      <KST_Kurz />
    </cEmployee>
  </T_Employee>
</import>

使用这个VB.NET类

 Public Class cEmployee
    <System.Xml.Serialization.XmlElement("MI_KZ")> _
    Public strMI_KZ As String = ""

    <System.Xml.Serialization.XmlElement("MI_Name")> _
    Public strMI_Name As String = ""

    <System.Xml.Serialization.XmlElement("MI_Vorname")> _
    Public strMI_Vorname As String = ""

    <System.Xml.Serialization.XmlElement("MI_Nummer")> _
    Public strMI_Nummer As String = ""

    <System.Xml.Serialization.XmlElement("MI_DatumVon")> _
    Public strMI_DatumVon As String = ""

    <System.Xml.Serialization.XmlElement("MI_DatumBis")> _
    Public strMI_DatumBis As String = ""

    <System.Xml.Serialization.XmlElement("AP_Bezeichnung")> _
    Public strAP_Bezeichnung As String = ""

    <System.Xml.Serialization.XmlElement("KOE_Code")> _
    Public strKOE_Code As String = ""

    <System.Xml.Serialization.XmlElement("KST_Code")> _
    Public strKST_Code As String = ""

    <System.Xml.Serialization.XmlElement("KST_Kurz")> _
    Public strKST_Kurz As String = ""
End Class


<System.Xml.Serialization.XmlRoot("import")> _
Public Class cImportXML
    <System.Xml.Serialization.XmlAttribute("date")> _
    Public generated As String = ""



    <System.Xml.Serialization.XmlArray("T_Employee")> _
    Public Mitarbeiter As List(Of cEmployee) = New List(Of cEmployee)

End Class

1 个答案:

答案 0 :(得分:3)

这里的一个技巧是xsd.exe:

xsd.exe sample.xml
xsd.exe sample.xsd /classes /language:VB

这应该为您生成兼容的类。但是,在这种情况下,具体问题是您需要[XmlElement],而不是[XmlArray]

<System.Xml.Serialization.XmlElementAttribute("T_Employee")> _
Public HelsanaMitarbeiter As List(Of cEmployee) = New List(Of cEmployee)