0

Looking to generate an XML file that looks something like this using XML Serialization:

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

I'm having trouble getting the FullName to repeat should there be more than one person. Would also like to know how to include attributes in the tags as well.

I've seen things like using XMLArray & XMLArrayItem properties, but not sure how to use these.

Could someone help me out in how to not only create the xml template, but also how the code to create the two FullName items? Every attempt I make I receive an Array Error.

EDIT:

This is what I'm currently trying to do using XmlArray, but get "Object reference not set to an instance of an object.":

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)

(I used the StringWriter so that once it's been serialized, I can throw it into a text file)

jnasty
  • 79
  • 1
  • 2
  • 9

1 Answers1

-1

how about,

Public Class FullName
End Class

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

then,

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
Jodrell
  • 34,946
  • 5
  • 87
  • 124