希望使用XML Serialization生成一个类似于此的XML文件:
<Root>
<Persons>
<FullName />
<FullName />
<Persons>
</Root>
如果有多个人,我无法重复FullName重复。还想知道如何在标签中包含属性。
我见过像使用XMLArray&amp ;; 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,这样一旦它被序列化,我就可以把它扔进一个文本文件中)
答案 0 :(得分:-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