ASP.NET/C# - Deserialize XML, XSD.exe created multiple classes
Posted
by Barryman9000
on Stack Overflow
See other posts from Stack Overflow
or by Barryman9000
Published on 2010-03-17T23:28:34Z
Indexed on
2010/03/17
23:31 UTC
Read the original article
Hit count: 516
I'm trying to deserialize some XML (returned from a web service) into an object that I created using XSD.exe. The XSD executable created a .CS file with a different partial class for each parent node in the XML.
For Example, the beginning of the XML looks like this (why can't I post xml here? sorry, this is ugly):
<disclaimer><notificationDescription>Additional charges will apply.</notificationDescription></disclaimer><quote><masterQuote></masterQuote></quote>
And the Class generated by the XSD.exe has a partial class named disclaimer with a get; set; string for notificationDescription. And another partial class for quoteMasterQuote with the corresponding child nodes as public strings.
How can I deserialize this XML file into multiple classes? I found this code, but it seems like it'll only work for one object.
public static PricingResponse2 DeSerialize(string _xml)
{
PricingResponse2 _resp = new PricingResponse2();
StringReader _strReader = new StringReader(_xml);
XmlSerializer _serializer = new XmlSerializer(_resp.GetType());
XmlReader _xmlReader = new XmlTextReader(_strReader);
try
{
_resp = (PricingResponse2)_serializer.Deserialize(_xmlReader);
return _resp;
}
catch (Exception ex)
{
string _error = ex.Message;
throw;
}
finally
{
_xmlReader.Close();
_strReader.Close();
_strReader.Dispose();
}
}
This is the first time I've tried this, so I'm a little lost.
© Stack Overflow or respective owner