c# - XML Deserialization of attributes -


i have complicated xml structure hierarchical process, stage, step, task follows:

<process>     <stage stagename="data gathering" stagepage="page_da.html" status="not started" selected="true">         <stageusers>             <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>             <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>         </stageusers>         <step stepname="data acquistion - general" steppage="page_dageneral.html" status="not started">             <stepusers>                 <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                 <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>             </stepusers>             <task taskname="well locations , reference elevations" tasktip="well locations , reference elevations" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="well deviation surveys" tasktip="well deviation surveys" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>         </step>         <step stepname="data acquistion - petrophysics" steppage="page_dapetro.html" status="not started">             <stepusers>                 <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                 <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>             </stepusers>             <task taskname="digitization of maps , logs" tasktip="digitization of maps , logs" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="log data loading" tasktip="log data loading" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="special core analysis" tasktip="special core analysis" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>         </step>         <step stepname="data acquistion - geology" steppage="page_dageology.html" status="not started">             <stepusers>                 <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                 <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>             </stepusers>             <task taskname="stratigraphic correlations" tasktip="stratigraphic correlations" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="core data" tasktip="core data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="geochemistry data" tasktip="geochemistry data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="dip interpretation data" tasktip="dip interpretation data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="existing digital time or depth structure maps" tasktip="existing digital time or depth structure maps" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="existing digital property maps" tasktip="existing digital property maps" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>         </step>         <step stepname="data acquistion - geophysics" steppage="page_dageophysics.html" status="not started">             <stepusers>                 <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                 <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>             </stepusers>             <task taskname="seismic data loading" tasktip="seismic data loading" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="stacking velocity ata loading" tasktip="stacking velocity ata loading" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="vsp or check shot data" tasktip="vsp or check shot data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>         </step>         <step stepname="data acquistion - engineering" steppage="page_daeng.html" status="not started">             <stepusers>                 <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                 <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>             </stepusers>             <task taskname="drilling , completion data" tasktip="drilling , completion data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="pressure transient data" tasktip="pressure transient data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="production data" tasktip="production data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="pressure data" tasktip="pressure data" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="sample chracterization" tasktip="sample chracterization" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>         </step>         <step stepname="data acquistion - additional" steppage="page_daadd.html" status="not started">             <stepusers>                 <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                 <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>             </stepusers>             <task taskname="new cores" tasktip="new cores" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="special core analysis geomechanics" tasktip="special core analysis geomechanics" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="core sampling" tasktip="core sampling" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>             <task taskname="laboratory tests" tasktip="laboratory tests" status="not started">                 <taskusers>                     <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>                     <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>                 </taskusers>             </task>         </step>     </stage>     <stage stagename="well reservoir production review" stagepage="page_wrpr.html" status="not started" selected="true">         <stageusers>             <user alias="pneelakandan" fullname="prasaanth neelakandan" role="dev" email="pneelakandan@exchange.com" organization="windows"/>             <user alias="sguo3" fullname="wen jiao" role="dev" email="sguo3@exchange.com" organization="windows"/>         </stageusers>     </stage> </process> 

basically , process made of stages . , stages made of steps , steps made of tasks. each of these have assigned users , attributes name , page , status .etc.

all of these have own classes :

public class process {     public string processname { get; set; }     public string helpfile { get; set; }     public list<stage> stages { get; set; }     public process()     {         stages = new list<stage>();     }      public xelement generatexml()     {         var proc = new xelement("process");          foreach (stage stg in stages)         {             proc.add(stg.generatexml());         }          // returns final process xml         return proc;     } } 

stage class :

public class stage {     public string stagename { get; set; }     public string stagepage { get; set; }     public list<step> steps { get; set; }     public list<user> stageusers { get; set; }     public string stagestatus { get; set; }      public bool isselected;      public stage(string name)     {         stagename = name;         steps = new list<step>();         stageusers = new list<user>();         selected = true;     }     public xelement generatexml()     {         var proj = new xelement("stage",             new xattribute("stagename", stagename),             new xattribute("stagepage", stagepage),             new xattribute("status", stagestatus),              new xattribute("selected", isselected)             );         var users = new xelement("stageusers");         foreach (user stguser in stageusers)         {             users.add(stguser.generatexml());         }         proj.add(users);          foreach (step st in steps)         {             proj.add(st.generatexml());         }         return proj;     } } 

step class :

public class step {     public string stepname { get; set; }     public string steppage { get; set; }     public list<task> tasks { get; set; }     public list<user> stepusers { get; set; }     public string stepstatus { get; set; }      public step(string name)     {         stepname = name;         tasks = new list<task>();         stepusers=new list<user>();     }      public xelement generatexml()     {         var proj = new xelement("step",             new xattribute("stepname", stepname),             new xattribute("steppage", steppage),             new xattribute("status", stepstatus)             );         var users = new xelement("stepusers");         foreach (user stpuser in stepusers)         {             users.add(stpuser.generatexml());         }         proj.add(users);          foreach (task tk in tasks)         {             proj.add(tk.generatexml());         }         return proj;     } } 

task class:

public class task {     public string tasktip { get; set; }     public string taskname { get; set; }     public list<user> taskusers { get; set; }     public string taskstatus { get; set; }     public task(string name)     {         taskname = name;         taskusers = new list<user>();     }      public xelement generatexml()     {         var proj = new xelement("task",             new xattribute("taskname", taskname),             new xattribute("tasktip", tasktip)            ,             new xattribute("status", taskstatus)             );          var users = new xelement("taskusers");         foreach (user tkuser in taskusers)         {             users.add(tkuser.generatexml());         }         proj.add(users);         return proj;     } } 

and user class :

public class user {     public string alias { get; set; }     public string fullname { get; set; }     public string email { get; set; }     public string role { get; set; }     public string organization { get; set; }      public xelement generatexml()     {         var proj = new xelement("user",           new xattribute("alias", alias),           new xattribute("fullname", fullname),           new xattribute("role", role),           new xattribute("email", email),           new xattribute("organization", organization)           );          return proj;     } } 

i have written generatexml() methods eahc step final xml displayed above (when called efdp user).

my question : there way can read above xml respective classes , objects shown ? tried hand @ xml serialization not figure out how attributes? try brute force way of reading objects lengthy.

if ideas on or if suggestions improve above design enable reading xml objects great.

public class reviewer {     [xmlattribute]     public string verdict { get; set; }      [xmlattribute]     public string comments { get; set; }      [xmlelement]     public user user { get; set; }      public reviewer()     {         user = new user();     } } 

you can use xmlserializer serialize property of class xml attribute applying [xmlattribute]: attribute property:

public class user {     [xmlattribute]     public string alias { get; set; }     [xmlattribute]     public string fullname { get; set; }     [xmlattribute]     public string email { get; set; }     [xmlattribute]     public string role { get; set; }     [xmlattribute]     public string organization { get; set; } } 

for more information on controlling , customizing xml serialization of .net classes, see here: attributes control xml serialization.

having done that, can serialize class xelement or xml string using extension methods such following:

public static class xmlserializationextensions {     public static string toxml<t>(this t obj)     {         return obj.toxml(false);     }      public static string toxml<t>(this t obj, bool omitstandardnamespaces)     {         using (var textwriter = new stringwriter())         {             var settings = new xmlwritersettings() { indent = true, indentchars = "  " }; // cosmetic purposes.             using (var writer = xmlwriter.create(textwriter, settings))             {                 xmlserializernamespaces ns = null;                 if (omitstandardnamespaces)                     (ns = new xmlserializernamespaces()).add("", ""); // disable xmlns:xsi , xmlns:xsd lines.                 new xmlserializer(obj.gettype()).serialize(writer, obj, ns);             }             return textwriter.tostring();         }     }      public static t deserializexml<t>(this string xmlstring)     {         using (var reader = new stringreader(xmlstring))         {             var result = new xmlserializer(typeof(t)).deserialize(reader);             if (result t)                 return (t)result;         }         return default(t);     }      public static xelement toxelement<t>(this t obj)     {         return obj.toxelement(true);     }      public static xelement toxelement<t>(this t obj, bool omitstandardnamespaces)     {         var doc = new xdocument();         using (var writer = doc.createwriter())         {             xmlserializernamespaces ns = null;             if (omitstandardnamespaces)                 (ns = new xmlserializernamespaces()).add("", ""); // disable xmlns:xsi , xmlns:xsd lines.             new xmlserializer(obj.gettype()).serialize(writer, obj, ns);         }         var element = doc.root;         if (element != null)             element.remove();         return element;     }      public static t deserializexml<t>(this xcontainer element)     {         using (var reader = element.createreader())         {             var result = new xmlserializer(typeof(t)).deserialize(reader);             if (result t)                 return (t)result;         }         return default(t);     } } 

then can use extension methods so:

        var user = new user { alias = "foo", email = "bar@none.net", fullname = "full name name", organization = "disorganized", role = "forward" };         var xml = user.toxml();         debug.writeline(xml);         var xelement = user.toxelement();         debug.writeline(xelement); 

or, deserialize:

        var process1 = xmlstring.deserializexml<process>();         var process2 = xmlelement.deserializexml<process>(); 

update

in order use xmlserializer, classes must have public (possibly default) parameterless constructor. also, in xml, of lists 1 level deep while 2 levels deep. one-level-deep lists need decorated xmlelement attribute, so:

    [xmlelement("stage")]     public list<stage> stages { get; set; } 

you have few name inconsistencies. these can fixed specifying element name or attribute name, instance:

    [xmlattribute("selected")]     public bool isselected { get; set; } 

thus following set of classes seem read in sample xml successfully:

public class process {     public string processname { get; set; }     public string helpfile { get; set; }     [xmlelement("stage")]     public list<stage> stages { get; set; }     public process()     {         stages = new list<stage>();     } }  public class stage {     [xmlattribute]     public string stagename { get; set; }     [xmlattribute("stagepage")]     public string stagepage { get; set; }     [xmlattribute("status")]     public string stagestatus { get; set; }     [xmlattribute("selected")]     public bool isselected { get; set; }      [xmlelement("step")]     public list<step> steps { get; set; }     public list<user> stageusers { get; set; }      public stage() : this(null) { }      public stage(string name)     {         stagename = name;         steps = new list<step>();         stageusers = new list<user>();         isselected = true;     } }  public class step {     [xmlattribute]     public string stepname { get; set; }     [xmlattribute]     public string steppage { get; set; }     [xmlattribute("status")]     public string stepstatus { get; set; }      [xmlelement("task")]     public list<task> tasks { get; set; }     public list<user> stepusers { get; set; }      public step() : this(null) { }      public step(string name)     {         stepname = name;         tasks = new list<task>();         stepusers = new list<user>();     } }  public class task {     [xmlattribute]     public string taskname { get; set; }     [xmlattribute]     public string tasktip { get; set; }     [xmlattribute("status")]     public string taskstatus { get; set; }      public list<user> taskusers { get; set; }      public task() : this(null) { }      public task(string name)     {         taskname = name;         taskusers = new list<user>();     } }  public class user {     [xmlattribute]     public string alias { get; set; }     [xmlattribute]     public string fullname { get; set; }     [xmlattribute]     public string email { get; set; }     [xmlattribute]     public string role { get; set; }     [xmlattribute]     public string organization { get; set; } } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -