Object Serialization

K

Keshav

Hi All,

I have a class by name "Participant" and it has properties such as
"Id" ,"Name",Type". Type is of type enum.
I am using xmlserializer to serialize. When i serialize

I should get
<?xml version="1.0" encoding="utf-8"?>
<Participant xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="2323-232-3434"
Name="Participant1">
<ParticipantType Type="HUMAN" />
</Participant>

but i get something like

<?xml version="1.0" encoding="utf-8"?>
<Participant xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="2323-232-3434"
Name="Participant1">
<Type="HUMAN" />
</Participant>

What's wrong with the following code


public enum PartcpntType
{HUMAN,ROLE,ORGANIZATIONAL_UNIT,RESOURCE,RESOURCESET,SYSTEM}
public class Participant
{


protected string _id;
protected string _name;
private PartcpntType partType;

[XmlAttribute("Id")]
/* sets or gets id */
public string Id
{
get
{return _id;}
set
{ _id = value;}
}

[XmlAttribute("Name")]
public string Name
{
get
{return _name;}
set
{_name = value;}
}

//[new XmlElement("ParticipantType")]
[XmlAttribute("Type")]
public WFParticipantType ParticipantType
{
get
{ return participanttype;}
set
{participanttype = value;}
}
}

Pls correct me if the above code is wrong. Do i need to write seperate class
for ParticipantType?

Some one pls help me.

Regds,
Keshav
 
C

Cezary Nolewajka

Not really. I think you can't get the result you expect with your code. The ParticipantType accessor value can be either an element or attribute of the Participant element. You want to nest it one level deeper. You want the value to be an attribute of an extra ParticipantType element:

<?xml version="1.0" encoding="utf-8"?>
<Participant xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="2323-232-3434"
Name="Participant1">
<ParticipantType Type="HUMAN" />
</Participant>

To achive that you need an additional type that wraps the value of PartcpntType enum.

So, the code would look like that:

public enum PartcpntType
{
HUMAN,ROLE,ORGANIZATIONAL_UNIT,RESOURCE,RESOURCESET,SYSTEM
}

public class ParticipantTypeWrap
{
[XmlAttribute("TYPE")]
public PartcpntType type;
}

public class Participant
{
protected string _id;
protected string _name;
//private PartcpntType partType;
private ParticipantTypeWrap partType;

[XmlAttribute("Id")]
/* sets or gets id */
public string Id
{
get
{return _id;}
set
{ _id = value;}
}

[XmlAttribute("Name")]
public string Name
{
get
{return _name;}
set
{_name = value;}
}

[XmlElement("ParticipantType")]
//[XmlAttribute("Type")]
public ParticipantTypeWrap ParticipantType
{
get
{ return partType;}
set
{partType = value;}
}
}

The serialization code:

obj.Id = "test id";
obj.Name = "test name";
ParticipantTypeWrap ptw = new ParticipantTypeWrap ();
ptw.type = PartcpntType.HUMAN;
obj.ParticipantType = ptw;

XmlSerializer xser = new XmlSerializer (typeof (Participant));

// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter("myFileName.xml");
xser.Serialize(myWriter, obj);

and the result you wanted:

<?xml version="1.0" encoding="utf-8" ?>
- <Participant xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="test id" Name="test name">
<ParticipantType TYPE="HUMAN" />
</Participant>

--
Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply

Keshav said:
Hi All,

I have a class by name "Participant" and it has properties such as
"Id" ,"Name",Type". Type is of type enum.
I am using xmlserializer to serialize. When i serialize
[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top