1

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

2
  • You have one light and one table per room so, to add another table, you need another room. Commented Jun 15, 2012 at 13:15
  • How can I instantiate another table object in the room list without getting a duplication error? Commented Jun 15, 2012 at 13:15

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Derp. I forgot about XmlElement's effect on collections.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.