HighTechTalks DotNet Forums  

serialize c# class with xsi:type attribute

Dotnet XML microsoft.public.dotnet.xml


Discuss serialize c# class with xsi:type attribute in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
bonnie
 
Posts: n/a

Default serialize c# class with xsi:type attribute - 12-10-2007 , 06:36 AM






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" /
Quote:
/bList
</classC>


Can anyone tell me how to do this?
Thanks in advance...

Bonnie


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.