我在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>

如果没有标签bs,我如何使用xmlement或xarray…在班上?

最佳答案

您应该能够通过对列表应用XmlElement attribute来实现此行为,而忽略XmlArrayXmlArrayItem

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

关于xml - 将模型类转换为XML,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46195444/

10-09 00:17