Serialization in C#

Posted by anjansaha on Stack Overflow See other posts from Stack Overflow or by anjansaha
Published on 2010-05-18T14:46:00Z Indexed on 2010/05/18 14:50 UTC
Read the original article Hit count: 270

Filed under:

My class structure is as follows.

[Serializable] [XmlRootAttribute("person", Namespace = "", IsNullable = false)] public class Person : IDisposable { Private int _id; Private string _name;

  [XmlElement(“id”)]
  Public int Id
  {
        Get{ return _id;}
        Set{ _id = value;}
  }

  [XmlElement(“name”)]
  Public string Name
  {
        Get{return _name;}
        Set{_name = value;}
  }

}

I am getting the following xml when I serialize the above class

<person> 
 <id>1</id>
  <name>Test</name>
</person>

Now, I would like to serialize the above class as follows i.e. I would like append “type” attribute for each public property that is serialized as xml element. I can append “type” attribute to “person” node by declaring another public property “type” with “[XmlAttribute(“type”)]” but I would like to achieve the same for each public property that is serialized as xml element. Any idea to achieve below:

<person type=”Person”>
  <id type=”int”>1</id>
  <name type=”string”>Test</name>
</person>

© Stack Overflow or respective owner

Related posts about c#2.0