user1324418
user1324418

Reputation: 395

C# XML Serialization of multiple objects of the same type within a single tag

I have the following XML that I would like to reproduce using xml serialization:

<Room>
<!-- One light-->  
<light Type="Incadenscent" fin="QS f" ItemType="something "/>
<!-- Unlimited Tables -->
<table Type="BR" Id="10"/>
<table Type="BL" Id="21"/>
<table Type="BR" Id="22"/>
<table Type="GR" Id="35"/>
<table Type="BR" Id="18"/>
<table Type="RE" Id="55"/>
</Room>

Below are my object types:

public class Table
{
    [XmlAttribute("type")]
    public string Type
    {
        get; set;
    }

    [XmlAttribute("Id")]
    public String Id
    {
        get; set;
    }

}

public class Light
{

    [XmlAttribute("type")]
    public string Type
    {
        get; set;
    }

    [XmlAttribute("fin")]
    public string FIN
    {
        get; set;
    }

    [XmlAttribute("ItemType")]
    public string ItemType
    {
        get; set;
    }
 }

public class Room{

      public Table Table
    {
        get; set;
    }

    public Light Light
    {
        get; set;
    }

  }

 public  class Program
{
    static void Main(string[] args)
    {

        List<Room> list = new List<Room>
        {
            new Room
            {

                Light = new Light{ Type="Incadenscent",  fin="QS", ItemType="something"},
                Table = new Table{Type="Metal", Id="10"}
                //error here when I try to add a new table object
                Table = new Table{Type="Wood", Id="13"}
            }
           } ;     
         SerializeToXML(list);

    }
    static public void SerializeToXML(List<Room> sample) 
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Room>)););
        TextWriter textWriter = new StreamWriter(@"C:\assets.xml");
        serializer.Serialize(textWriter, sample);
        textWriter.Close();

    }
}

I get an error(specifically-duplication of object) when I try to instantiate another table object within the Room object. What am I doing wrong?

for example:

                **Table = new Table{Type="Wood", Id="13"}**

How can I instantiate another table object in the room list without getting a duplication error

Upvotes: 1

Views: 4374

Answers (2)

Tisho
Tisho

Reputation: 8502

There is a simple solution for that:

public class Room
{
     [XmlElement("light")]
     public Light Light { get; set; }
     [XmlElement("table")]
     public List<Table> Tables { get; set; }
}

The initialization is as described by @HackedByChinese's answer.

Declare the List as [XmlElement], then it will not serialize the <tables> element, and the xml will look exactly like you want.

Upvotes: 4

moribvndvs
moribvndvs

Reputation: 42495

Your XML doesn't match the class. Room declares that it contains one Light and one Table, where the XML has multiple Tables.

Room should look more like:

public class Room
{
     public Light Light { get; set; }
     public List<Table> Tables { get; set; }
}

and create the object like this:

    new Room
    {

        Light = new Light{ Type="Incadenscent",  fin="QS", ItemType="something"},
        Tables = new List<Table>{ new Table{Type="Metal", Id="10"},
                                  new Table{Type="Wood", Id="13"} }
    }

However, you still will have deserialization problems. XmlSerializer will expect the XML to look more like:

<Room>
    <light Type="Incadenscent" fin="QS f" ItemType="something "/>
    <tables>
       <table Type="BR" Id="10"/>
       <table Type="BL" Id="21"/>
       <table Type="BR" Id="22"/>
       <table Type="GR" Id="35"/>
       <table Type="BR" Id="18"/>
       <table Type="RE" Id="55"/>
     </tables>
</Room>

However, if the resulting XML must look the way you specified in your example, you will need to implement IXmlSerializable on Table, and use the XmlReader and XmlWriter to deserialize and serialize (respectively) manually.

Upvotes: 0

Related Questions