c# - Serialize List<Object> that property of list item is element name in XML -


i don't know if topic correct, if not please correct. far not sure search problem maybe question has been answered before.

currently have following class (as example):

[serializable] public class sample {     public string { get; set; }     public list<parameter> parameters { get; set; } }   [serializable] public class parameter {     public string name { get; set; }     public string value { get; set; } } 

this structure have serialize following xml:

<sample>    <something>1234512345112345</something>      <parameters>      <name>value</name>      <name>value</name>    </parameters>  </sample> 

so xml should contain property value of attribute "name" xml-element name.

update 20.05.2015 have following xml content:

 <?xml version="1.0" encoding="utf-16" ?>   <processdata>  <id>123456</id>    <idytpe>baseplate</idytpe>    <state>fail</state>    <recipe>654321</recipe>   <processdataparameter>   <test_0>0</test_0>    <test_1>12,34</test_1>    <test_2>24,68</test_2>    <test_3>37,02</test_3>    <test_4>49,36</test_4>    <test_5>61,7</test_5>    </processdataparameter>   </processdata> 

when try use following code deserialize:

 public void readxml(xmlreader reader)         {             reader.readstartelement("processdata");              this.id = reader.readelementstring("id");             this.idtype = reader.readelementstring("idytpe");             this.state = reader.readelementstring("state");             this.recipe = reader.readelementstring("recipe");              reader.readstartelement("processdataparameter");             this.processdataparameter = new list<processdataparameter>();              var subtree = reader.readsubtree();              while (subtree.read())             {                 if (subtree.nodetype == xmlnodetype.text)                 {                     var nm = subtree.localname;                     //parameters.add(new parameter { name = nm, value = subtree.value });                  }             }              reader.readendelement();          } 

everything gets read out fine expect process data parameters.

it seems subtree.read() reades element out of xml content instead of elements contained in .

in while loop reader goes through following values (debuged)

  • test_0 (start tag)
  • 0 (value between tag)
  • test_0 (end tag

and out of while. seems reader sees subtree. further 0 - value gets recognized xmlnodetype.text

you implement ixmlserializable , create own custom serialization behaviour sample class. in case should work

[serializable] public class sample : ixmlserializable {     public string { get; set; }     public list<parameter> parameters { get; set; }      public xmlschema getschema()     {         return null;     }      public void readxml(xmlreader reader)     {         xmldocument doc = new xmldocument();         doc.load(reader);          = doc.selectsinglenode(@"/sample/something").firstchild.value;          var parameters = doc.selectsinglenode(@"/sample/parameters");         if (parameters.haschildnodes)         {             parameters = new list<parameter>();             foreach (xmlelement childnode in parameters.childnodes)             {                 parameters.add(new parameter {name = childnode.localname, value = childnode.firstchild.value});             }         }     }      public void writexml(xmlwriter writer)     {         writer.writeelementstring("something", this.something);         writer.writestartelement("parameters");         foreach (var parameter in parameters)         {             writer.writeelementstring(parameter.name, parameter.value);         }         writer.writeendelement();     } } 

updated include readxml implementation deserialization i'm not quite sure if readxml complete can't test now, might have tweak bit parameters


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -