【问题标题】:Serialize Array序列化数组
【发布时间】:2013-09-20 15:28:34
【问题描述】:

希望使用 XML 序列化生成一个看起来像这样的 XML 文件:

<Root>
 <Persons>
  <FullName />
  <FullName />
 <Persons>
</Root>

如果有超过一个人,我无法让 FullName 重复。也想知道如何在标签中包含属性。

我见过使用 XMLArray 和 XMLArrayItem 属性之类的东西,但不知道如何使用它们。

有人可以帮助我了解如何不仅创建 xml 模板,而且还可以帮助我创建两个 FullName 项的代码吗?我每次尝试都会收到一个数组错误。

编辑:

这是我目前正在尝试使用 XmlArray 执行的操作,但得到“对象引用未设置为对象的实例。”:

Public Class Root
 <XmlArrayItem("fullName")>
 Public Property first As String()
End Class

Dim x As New Root
x.first(0) = "john"
x.first(1) = "james"

Dim serializer As New XmlSerializer(GetType(Root))
Dim writer As New System.IO.StringWriter
serializer.Serialize(writer, x)

(我使用了StringWriter,这样一旦它被序列化,我就可以将它放入一个文本文件中)

【问题讨论】:

标签: .net xml vb.net serialization


【解决方案1】:

怎么样,

Public Class FullName
End Class

Public Class Root
    Public Property Persons As List(Of FullName)
End Class

那么,

Dim doc = New Root With { .Persons = { New FullName(), New FullName() } }

Dim serialiser = New XmlSerializer(doc.GetType())
Dim xml = string.Empty

Using writer As New StringWriter()
    serialiser.Serialize(writer, doc)
    xml = writer.ToString()
End Using

【讨论】:

  • 你有机会用VB重写吗?不知道如何实现,否则......
  • @jnasty 类似的东西
猜你喜欢
  • 2011-08-11
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
相关资源
最近更新 更多