This may be a simple question, but I'm struggling to find the answer
to it, so any suggestions would be greatly appreciated.... I want to
know if there's some way to ensure that the xsi:type attribute is
included when serializing a c# class.
The basic situation is this:
I have a class, ClassB which derives from ClassA.
ClassC contains a list of ClassB, which I want to serialize as an
XmlArray of 'bListItem's as per the code below:
[Serializable]
[XmlType("classA")]
public class ClassA
{
private string _id;
[XmlAttribute("id")]
public string Id
{
get { return _id; }
set { _id = value; }
}
}
[Serializable]
[XmlType("classA.classB")]
public class ClassB : ClassA
{
private string _name;
[XmlAttribute("name")]
public string Name
{
get { return _name; }
set { _name = value; }
}
}
[Serializable]
[XmlType("classC")]
public class ClassC
{
private string _id;
private List<ClassB> _bList;
[XmlAttribute("id")]
public string Id
{
get { return _id; }
set { _id = value; }
}
[XmlArray("bList"), XmlArrayItem("bListItem")]
public List<ClassB> BList
{
get { return _bList; }
set { _bList = value; }
}
}
Now... this serializes to the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<classC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="C1">
<bList>
<bListItem id="B1" name="My B Item" />
</bList>
</classC>
What I want to do is have the xsi:type of bListItem come through in
this element like so:
<?xml version="1.0" encoding="utf-8" ?>
<classC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="C1">
<bList>
<bListItem xsi:type="classA.classB" id="B1" name="My B Item" /
</classC>
Can anyone tell me how to do this?
Thanks in advance...
Bonnie