amr-it
asked on
DeSerialization - Element child nodes to string
Hello Experts,
Assume I want to deserialize the following XML:
I would like to deserialize this xml, but let the payload element outerxml (element + childnodes) to be stored as a string in the PayLoad property.
Example use:
Is this possible?
Thank you,
amr-it
Assume I want to deserialize the following XML:
<ROOT>
<STATUS>101</STATUS>
<PAYLOAD>
<PERSON>
<FIRSTNAME>xxx</FIRSTNAME>
<....>xxx</....>
</PERSON>
</PAYLOAD>
</ROOT>
I would like to deserialize this xml, but let the payload element outerxml (element + childnodes) to be stored as a string in the PayLoad property.
[XmlRoot(ElementName="ROOT")]
public class Example
{
private string status;
private string payLoad;
[XmlElement(ElementName = "PAYLOAD")]
public string PayLoad
{
get { return payLoad; }
set { payLoad= value.}
}
[XmlElement(ElementName = "STATUS")]
public string Status
{
get { return status; }
set { status = value; }
}
}
Example use:
Example MyExample = serializer.Deserialize(xmlstring);
Console.WriteLine(MyExample.PayLoad);
//Writes:
//<PAYLOAD><PERSON><FIRSTNAME>xxx</FIRSTNAME><....>xxx</....></PERSON></PAYLOAD>
Is this possible?
Thank you,
amr-it
ASKER
Thank you kaufmed for your comment.
I forgot to mention that my serializer and deserializer methods are located in a helper class and are designed to be generic (handling many different types), meaning if possible, I don't want to cast to the Example type. I got it to work doing something very similair to what you have proposed and I would assume that the above would work.
But, is there any way to define this in the class design so that the deserializer directly knows how to handle it?
I forgot to mention that my serializer and deserializer methods are located in a helper class and are designed to be generic (handling many different types), meaning if possible, I don't want to cast to the Example type. I got it to work doing something very similair to what you have proposed and I would assume that the above would work.
But, is there any way to define this in the class design so that the deserializer directly knows how to handle it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Comment for accepting own solution + expert:
Hopefully this generic solution also can be used for someone.
Hopefully this generic solution also can be used for someone.
Open in new window