C# .NET - Deserialize xml string to custom objects

Asked By Nadaraja Yogendran on 02-May-09 12:44 PM

I retrieve dataset from dataaccess layer and construct xml string

e.g:

<Videos>
<Video ISBN="0-7888-1623-3">
<Title Screenplay="Marty Kaplan">The Distinguished Gentleman</Title>
<Director>Jonathan Lynn</Director>
<Actors>
</Actors>
<Length>112 Minutes</Length>
<Format>DVD</Format>
<Rating>R</Rating>
</Video>
<Video>
<Title WrittenBy="Charlie Peter">Her Alibi</Title>
<Director>Bruce Beresford</Director>
<Length>94 Mins</Length>
<Format>DVD</Format>
<Rating>PG-13</Rating>
</Video>
</Videos>

this depends on totally  the Database View we choose in the DAL

Some other time this would be from Employee table.

<Employees>

<Employee>

<FirstName>Me</FirstName>

<LastName>Me Too</LastName>

</Employee>

<Employee>

<FirstName>Not Me</FirstName>

<LastName>Not Me Too</LastName>

</Employee>

</Employees>


Now the question is:

How can I deserialize this xml string  to objects in runtime? Anyway, Is it Possible???

[Problem is I don't know what this xml string would look like before hand]


Thanks in Advance

~Yogi

Santhosh N replied to Nadaraja Yogendran on 02-May-09 01:16 PM
The answer to your question in yes...

You can use XmlSerializer.Deserialize Method to do this...
For this, you can actually load the xml string to an xmldocument if not available as an xmldocument...

Check this one explaining how to use this..

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.deserialize(VS.71).aspx

and here is another example...

http://forums.asp.net/t/1054512.aspx

a simple approach to this problem

H K replied to Nadaraja Yogendran on 02-May-09 10:06 PM
One of the simple approach to the problem is before deserialzing,  as you have the access to the xml string of the object, you can  use xml dom object  to identify the root element of the xml string and have a switch case for all the kinds of object you are going to get from the database.

Based on the root node name, for example if the root node name is Employee then you can deserialze the object to the appropriate type by type casting it.


XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);

string rootNode =  doc.FirstNode().Name;
//Code for deserializing the xml
XmlSerializer mySerializer ;

swicth (rootNode)
{
             Case "Employee":
             //deserialize to employee type
            mySerializer = new XmlSerializer(typeof(Employee));
            break;
            Case "Videos":
            //deserialize to Videos type
            mySerializer = new XmlSerializer(typeof(Videos));
            break;
            default: 
             //return invalid type
       
}

object = mySerializer.Deserialize(myStream);

above is not a compiling code, but just a kind of pseudo code..
Hope this solves your problem.

Yep..

Nadaraja Yogendran replied to H K on 03-May-09 08:55 AM

This would be solving my issues...

at the same time I'm looking at this DLR too.

thnx HK

welcome
H K replied to Nadaraja Yogendran on 03-May-09 01:05 PM
welcome yogendra, if the post was really helpful, can you mark it as helpful..

thanks
Yep... I did :)
Nadaraja Yogendran replied to H K on 04-May-09 03:05 PM
end of post
Deserializing Xml into object
Sukanya Venkatasubramanian replied to Santhosh N on 16-Sep-09 02:35 AM

Hi,

Thanks for your valuable suggestion given to Yogi to deserialize the Xml into object.

I have a problem when anyof the Xml node contains Date values. When I deserialize the Xml with date values, it throws exception.

Could anyone help me to resolve this issue?

Many thanks.

Regards,

Sukanya

Isuppose
Santhosh N replied to Sukanya Venkatasubramanian on 16-Sep-09 09:26 AM
the xsd is strict on this and you need to modify the xsd to allow any string...