1

If I wanted to serizlize and deserialize an XML like this in VB:

  <SeveralListsOfPeople>
      <ListOfPeople groupname="Friends">
          <Person name="John">
          </Person>
          <Person name="Mary">
          </Person>
      </ListOfPeople>
      <ListOfPeople groupname="Family">
          <Person name="Karen">
          </Person>
          <Person name="Kyle">
          </Person>
          </ListOfPeople>
      <ListOfPeople groupname="Enemies">
          <Person name="Ben">
          </Person>
          <Person name="Chris">
          </Person>
      </ListOfPeople>
    </SeveralListsOfPeople>

How would I add the attribute "groupname" to "ListsOfPeople"? I can't figure out how to add attributes to lists or arrays. I only know how to add attributes to objects and the root.

This is how I thought it should work:

Imports System.Xml.Serialization

<Serializable>
<XmlRoot("SeveralListsOfPeople")>
Public Class SeveralListsOfPeople
    Public Property ListOfPeople As List(Of Person)
    <XmlAttribute("groupname")> Public Property name() As String
    Public Sub New()
    End Sub
End Class

<Serializable>
Public Class Person
    Public Property title As String
    Public Property age As String
    <XmlAttribute("name")> Public Property name() As String
    Public Sub New()
    End Sub
End Class
SeanO
  • 11
  • 3
  • It would help if you could show us the code you're using for what you know. – CoderDennis Feb 17 '15 at 07:07
  • Your class `SeveralListOfPeople` doesn't look like it has a collection of lists. How are you representing more than one `ListOfPeople` nodes in each `SeveralListOfPeople`? – CoderDennis Feb 17 '15 at 16:54

1 Answers1

0

Based on this answer to a similar question, you'd need to create your own PeopleCollection class and implement IXmlSerializable to get this to work with System.Xml.Serialization.

However, since this is vb.net and we have XML Literals, I prefer using that and defining ToXml and FromXml methods for my classes.

This will give you what you're looking for without fighting with implementing IXmlSerializable.

Module Module1

Sub Main()
    Dim xml = <SeveralListsOfPeople>
                  <ListOfPeople groupname="Friends">
                      <Person name="John">
                      </Person>
                      <Person name="Mary">
                      </Person>
                  </ListOfPeople>
                  <ListOfPeople groupname="Family">
                      <Person name="Karen">
                      </Person>
                      <Person name="Kyle">
                      </Person>
                  </ListOfPeople>
                  <ListOfPeople groupname="Enemies">
                      <Person name="Ben">
                      </Person>
                      <Person name="Chris">
                      </Person>
                  </ListOfPeople>
              </SeveralListsOfPeople>

    Dim test = SeveralListsOfPeople.FromXml(xml)

    Console.WriteLine(test.ToXml)
    Console.ReadKey()
End Sub

Public Class SeveralListsOfPeople
    Public Property Lists As List(Of ListOfPeople)
    Public Function ToXml() As XElement
        Return <SeveralListsOfPeople>
                   <%= From l In Lists
                       Select x = l.ToXml %>
               </SeveralListsOfPeople>
    End Function
    Public Shared Function FromXml(ByVal xml As XElement) As SeveralListsOfPeople
        Return New SeveralListsOfPeople With
               {
                   .Lists = (From l In xml...<ListOfPeople>
                            Select ListOfPeople.FromXml(l)).ToList
               }
    End Function
End Class

Public Class ListOfPeople
    Public Property ListOfPeople As List(Of Person)
    Public Property name() As String
    Public Function ToXml() As XElement
        Return <ListOfPeople groupname=<%= name %>>
                   <%= From p In ListOfPeople
                       Select x = p.ToXml %>
               </ListOfPeople>
    End Function
    Public Shared Function FromXml(ByVal xml As XElement) As ListOfPeople
        Return New ListOfPeople With
               {
                   .name = xml.@groupname,
                   .ListOfPeople = (From p In xml...<Person>
                                    Select Person.FromXml(p)).ToList
               }
    End Function
End Class

Public Class Person
    Public Property title As String
    Public Property age As String
    Public Property name() As String
    Public Function ToXml() As XElement
        Return <Person name=<%= name %>>
                   <title><%= title %></title>
                   <age><%= age %></age>
               </Person>
    End Function
    Public Shared Function FromXml(ByVal xml As XElement) As Person
        Return New Person With
               {
                   .name = xml.@name,
                   .title = xml.<title>.Value,
                   .age = xml.<age>.Value
               }
    End Function

End Class

End Module
Community
  • 1
  • 1
CoderDennis
  • 13,642
  • 9
  • 69
  • 105