how can deserialize xml this:
<query> <parameters> <param name="lastupdate">2012-05-25</param> <param name="code">11222122</param> <param name="type">idnlookup</param> </parameters> <response> <category name="person" version="1"> <field name="surname">soap</field> <field name="name1">joe</field> <field name="date_of_birth">1973-05-09</field> </category> <category name="contact" version="1"> <row> <field name="phone">0118063433</field> <field name="type">home</field> <field name="date">2003-01-01</field> </row> <row> <field name="phone">0124666566</field> <field name="type">home</field> <field name="date">2008-03-11</field> </row> </category> </response> </query>
into class structure this:
public class query{ public string lastupdate {get;set;} public string code {get;set;} public string type {get;set;} public response response {get;set;} } class response{ public person person {get;set;} public contact[] contacts {get;set;} } class person { public string surname {get;set;} public string name1 {get;set;} public string date_of_birth {get;set;} } class contact { public string phone {get;set;} public string type {get;set;} public string date {get;set;} }
using standard .net xml serializer. or need roll own?
to expand on simon's xslt idea, consider following xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="/query/parameters"> <xsl:apply-templates select="*"/> <!-- removes level hierarchy applying directly --> </xsl:template> <xsl:template match="*[@name]"> <xsl:element name="{@name}"> <xsl:apply-templates select="@*[name()!='name'] | * | text()"/> </xsl:element> </xsl:template> </xsl:stylesheet>
the key match here *[@name]
, takes of form:
<foo name="abc" ...>...</foo>
and rewrites as:
<abc ...>...</abc>
this transforms xml to:
<query> <lastupdate>2012-05-25</lastupdate> <code>11222122</code> <type>idnlookup</type> <response> <person version="1"> <surname>soap</surname> <name1>joe</name1> <date_of_birth>1973-05-09</date_of_birth> </person> <contact version="1"> <row> <phone>0118063433</phone> <type>home</type> <date>2003-01-01</date> </row> <row> <phone>0124666566</phone> <type>home</type> <date>2008-03-11</date> </row> </contact> </response> </query>
which can mapped model minor tweaks attributes:
[xmlroot("query")] // <==== add public class query {...} [xmlarray("contact"), xmlarrayitem("row")] // <=== add public contact[] contacts { get; set; }
which can used, example:
static void main() { xslcompiledtransform xslt = new xslcompiledtransform(); xslt.load("my.xslt"); var sw = new stringwriter(); xslt.transform("my.xml", null, sw); var transformedxml = sw.tostring(); console.writeline(transformedxml); query query; using (var reader = xmlreader.create(new stringreader(transformedxml))) { query = (query)new xmlserializer(typeof(query)).deserialize(reader); } // query populated }
Comments
Post a Comment