使用VB.net反序列化XML

时间:2018-05-20 04:19:16

标签: xml vb.net

我成功地反序列化了这个XML,但是当我添加元数据部分时,如下所示,我不知道如何在代码中反序列化。我的意思是我不知道如何构建我的类属性,方法,归属标签等。

我尝试了很多组合,但反序列化后我的课程中的元数据仍然是空的。甚至不确定我是否提出正确的问题,而且我的代码有点冗长。我试着在这里粘贴它,但它说它太长了所以我不确定如何将所有这些粘贴给任何人看。

<?xml version="1.0" encoding="utf-8"?>
<MainMenu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MenuItems MenuGroup="Boilers">
    <SubMenuItem>
      <GUID>3ec6e759-270f-4cd1-a9b8-07b4c227d47b</GUID>
      <ComponentType>Boiler:HotWater</ComponentType>
      <DefaultName>Boiler - Hot Water</DefaultName>
      <EquipmentUsage>Supply</EquipmentUsage>
      <Picture>iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAABGdBTUEAALGPC/xhBQAABFdJREFUeF7tm89PE1EQx9vyQ8SCNSLWH5BGDWli1CKGFBRFMYgJBxIvmHggMfHcxANcSI8c/TM8cuTIyXD<Picture>
      <ImageName>hot_water_boiler</ImageName>
    </SubMenuItem>
    <SubMenuItem>
      <GUID>1a4ee747-c11b-4687-be41-73680c104934</GUID>
      <ComponentType>Boiler:Steam</ComponentType>
      <DefaultName>Boiler - Steam</DefaultName>

<!-- How to deserialize this Metadata section? -->

      <Metadata>
        <MetadataItem>
          <description>Design Flow Rate {m3/s}</description>
          <value>Autosize</value>
        </MetadataItem>
        <MetadataItem>
          <description>Design Pump Head {Pa}</description>
          <value>179352</value>
        </MetadataItem>
      </Metadata>

      <EquipmentUsage>Supply</EquipmentUsage>
      <Picture>iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAABGdBTUEAALGPC/xhBQAABFdJREFUeF7tm89PE1EQx9vyQ8SCNSLWH5BGDWli1CKGFBRFMYgJBxIvmHggMfHcxANcSI8c/TM8cuTIyXD0yNGjR44e1/mU3bK7vIVd7Nvuq2+ST3ZT2O7M972ZnX27zVmzZs1aj1peKLj0JcA7huN7y7a3t53NzU2<Picture>
      <ImageName>hot_water_boiler</ImageName>
    </SubMenuItem>
  </MenuItems>
</MainMenu>

代码我添加了list属性,

<XmlType("MenuItem")>
Public Class PECSubMenuItem

'NOTE: By NOT specifing any custom Metadata Attributes, fields will be created as an element by default.
 Private mGUID As String
 Private mPicture As Bitmap
 Private mMenuGroup As String
 Private mComponentType As String
 Private mDefaultName As String
 Private mImageName As String
 Private mEquipmentUsage As String
 Private mMetadataList As List(Of MetadataItem)
 Public Enum EquipUsage
  Supply = 0
  Demand = 1
  Both = 2
 End Enum
 Public Sub New()
  mGUID = System.Guid.NewGuid.ToString
  mMetadataList = New List(Of MetadataItem)
 End Sub
 <XmlElement("Metadata")>
 Public Property Metadata As List(Of MetadataItem)

  Get
   Return mMetadataList
  End Get
  Set(ByVal value As List(Of MetadataItem))
   mMetadataList = value
  End Set

 End Property

我为新课程添加了代码。

<XmlType("MetadataItem")>
Public Class MetadataItem

 Private mvalue As String
 Private mdescription As String
 <XmlElement("description")>
 Public Property description As String

  Get
   Return mdescription
  End Get
  Set(ByVal value As String)
   mdescription = value
  End Set

 End Property
 <XmlElement("value")>
 Public Property value As String

  Get
   Return mvalue
  End Get
  Set(ByVal value As String)
   mvalue = value
  End Set

 End Property

End Class

1 个答案:

答案 0 :(得分:0)

您需要另一个名为MetadataMetadataItem的课程:

<XmlType("MetadataItem")>
Public Class MetadataItem

    <XmlElement("description")>
    Public Property Description As String

    <XmlElement("value")>
    Public Property Value As String

    Public Sub New()
    End Sub

    Public Sub New(ByVal Description As String, ByVal Value As String)
        Me.Description = Description
        Me.Value = Value
    End Sub
End Class

然后在父类中,您需要一个属性,该属性是MetadataItem类的列表。使用XmlArrayXmlArrayItem属性指定列表的名称及其项目:

<XmlArray("Metadata"), XmlArrayItem("MetadataItem")>
Public Property Metadata As List(Of MetadataItem)

您的XML也存在问题:<Picture>标记永远不会关闭。

现在您的XML看起来像:

<Picture>...<Picture>

但它应该看起来像:

<Picture>...</Picture>