c# - Map XML nodes to object's properties easily -


returning ruby world xml-to-hash mapping easiness looking forward see similar in c#.

here source xml:

<whois-resources xmlns:xlink="http://www.w3.org/1999/xlink">     <service name="search"/>     <parameters>         <inverse-lookup/>         <type-filters/>         <flags/>         <query-strings>             <query-string value="....."/>         </query-strings>         <sources/>     </parameters>     <objects>         <object type="inetnum">             <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/inetnum/....."/>             <source id="ripe"/>             <primary-key>                 <attribute name="inetnum" value="....."/>             </primary-key>             <attributes>                 <attribute name="inetnum" value="....."/>                 <attribute name="netname" value="....."/>                 <attribute name="descr" value="....."/>                 <attribute name="descr" value="....."/>                 <attribute name="country" value="....."/>                 <attribute name="admin-c" value="....." referenced-type="person">                     <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/person/....."/>                 </attribute>                 <attribute name="tech-c" value="....." referenced-type="person">                     <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/person/....."/>                 </attribute>                 <attribute name="status" value="assigned pa"/>                 <attribute name="mnt-by" value="....." referenced-type="mntner">                     <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/mntner/....."/>                 </attribute>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="source" value="....." comment="filtered"/>             </attributes>             <tags>                 <tag id="ripe-user-resource"/>             </tags>         </object>         <object type="person">             <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/person/....."/>             <source id="ripe"/>             <primary-key>                 <attribute name="nic-hdl" value="....."/>             </primary-key>             <attributes>                 <attribute name="person" value="....."/>                 <attribute name="address" value="....."/>                 <attribute name="address" value="....."/>                 <attribute name="address" value="....."/>                 <attribute name="phone" value="....."/>                 <attribute name="fax-no" value="....."/>                 <attribute name="nic-hdl" value="....."/>                 <attribute name="mnt-by" value="....." referenced-type="mntner">                     <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/mntner/....."/>                 </attribute>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="source" value="....." comment="filtered"/>             </attributes>         </object>         <object type="route">             <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/route/....."/>             <source id="ripe"/>             <primary-key>                 <attribute name="route" value="....."/>                 <attribute name="origin" value="....."/>             </primary-key>             <attributes>                 <attribute name="route" value="....."/>                 <attribute name="descr" value="....."/>                 <attribute name="origin" value="....." referenced-type="aut-num">                     <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/aut-num/....."/>                 </attribute>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="remarks" value="....."/>                 <attribute name="mnt-by" value="....." referenced-type="mntner">                     <link xlink:type="locator" xlink:href="http://rest.db.ripe.net/ripe/mntner/....."/>                 </attribute>                 <attribute name="source" value="....." comment="filtered"/>             </attributes>         </object>     </objects>     <terms-and-conditions xlink:type="locator" xlink:href="http://www.ripe.net/db/support/db-terms-conditions.pdf"/> </whois-resources> 

which remap object following structure:

{     service: {         name: "....."     },     parameters: {         querystrings: [".....", "....."]     },     inetnum: {         link: {             type: ".....",             href: "....."         },         source: {             id: "....."         },         primarykey: {             inetnum: "....."         },         attr: {             inetnum: ".....",             netname: ".....",             descr: ".....",             country: ".....",             adminc: {                 value: ".....",                 referencedtype: ".....",                 link: {                     type: ".....",                     href: "....."                 }             },             techc: {                 value: ".....",                 referencedtype: ".....",                 link: {                     type: ".....",                     href: "....."                 }             },             status: ".....",             mntby: {                 value: ".....",                 referencedtype: ".....",                 link: {                     type: ".....",                     href: "....."                 }             }             remarks: ".....", #a concatenation of remarks nodes             source: {                 value: ".....",                 comment: "....."             }         },         tags: [             { id: "....." }         ]     },     person {         link: {             type: ".....",             href: "....."         },         source: {             id: "....."         }         primarykey: {             nichdl: "....."         },         attr: {             person: ".....",             address: ".....", #a concatenation of address nodes             phone: ".....",             faxno: ".....",             nichdl: ".....",             mntby: {                 value: ".....",                 referencedtype: ".....",                 link: {                     type: ".....",                     href: "....."                 }             }             remarks: ".....", #a concatenation of remarks nodes             source: {                 value: ".....",                 comment: "....."             }         }     },       route: {         link: {             type: ".....",             href: "....."         },         source: {             id: "....."         },         primarykey: {             route: ".....",             origin: "....."         },         attr: {             route: ".....",             descr: ".....",             origin: {                 value: ".....",                 referencedtype: ".....",                 link: {                     type: ".....",                     href: "....."                 }             },             remarks: ".....", #a concatenation of remarks nodes             mntby: {                 value: ".....",                 referencedtype: ".....",                 link: {                     type: ".....",                     href: "....."                 }             },             source: {                 value: ".....",                 comment: "....."             }         }     },     termsandconditions: {         type: ".....",         href: "....."     } } 

to able call properties like:

whois.inetnum.attr.netname 

i dislike idea retrieve each node calling selectsinglenode("xpath");

the desired process is:

  1. define receiver object , subobjects
  2. define mapping rules
  3. parse xml string single method call

here's way of doing it.

  1. you'll need add relevant references first (here's few):

    using system.linq;

    using system.xml.linq;

    using system.text;

    using system.xml;

    using system.xml.serialization;

  2. create wrapper class hold deserialized data.

    [xmlroot("root")] public class root {     [xmlelement("myobject")]     public list<myobject> a_object     {         get;         set;     } } 
  3. then class hold deserialized data, example here it's myobject:

    public class myobject {

    [xmlelement("type")] //from sample code route,person go here public string objtype {     get;     set; } [xmlelement("address")] public string personaddr {     get;     set; } [xmlelement("phone")] public string personphone {     get;     set; } 

    }

*note: if have properties want keep track of, not deserialized can have properites not have [xmlelement] there.

  1. finally deserialize xml list of objects:

public list<myobject> deserializexml(string xmlpath) { xmlserializer deserializer = new xmlserializer(typeof(root)); list<myobject> objectlist = new list<myobject>(); try { using (textreader textreader = new streamreader(xmlpath)) { root setofobjects; setofobjects = (root)deserializer.deserialize(textreader); } objectlist = setofobjects.a_object; } catch(filenotfoundexception a) { } return objectlist; } 5. can go ahead , manipulate whichever form like, maybe serialize differently again.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -