将模型类转换为XML

时间:2017-09-13 10:44:32

标签: xml vb.net class models

我在vb.net中有下一个课程:

Class A 
    Public Property Bs As New List(Of B)
End Class

Class B
    Public Property D As String
    Public Property E As String
End class

我希望使用下一代码在xml中转换它们:

Dim sw1 = New StringWriter()
Dim xs1 As New XmlSerializer(A.GetType)
xs1.Serialize(New XmlTextWriter(sw1), A)
xml = xml.Replace("{1}", sw1.ToString())

该类的格式为:

<A>
  <Bs>
    <B>
      <D>1</D>
      <E>2</E>
    </B>
    <B>
      <D>3</D>
      <E>2</E>
    </B>
</Bs>
</A>

但我希望有下一个:

<A>
    <B>
      <D>1</D>
      <E>2</E>
    </B>
    <B>
      <D>3</D>
      <E>2</E>
    </B>
</A>

如果没有标签B,我怎样才能在课堂上使用XmlElement或XArray?

1 个答案:

答案 0 :(得分:1)

您应该能够通过将XmlElement attribute应用到列表中来忽略XmlArrayXmlArrayItem来实现此行为:

Class A
    <XmlElement("B")> _
    Public Property Bs As New List(Of B)
End Class