c# - Converting XML to objects with nested embeded objects -


i have following xml has nested objects , when want deserialize xml steps class embedded list of step objects, have error.

[xmlroot(elementname="comp")] public class comp  {     [xmlattribute(attributename="ref")]     public string ref { get; set; }     [xmlattribute(attributename="dir")]     public string dir { get; set; } }  [xmlroot(elementname="step")] public class step  {     [xmlelement(elementname="comp")]     public comp comp { get; set; }     [xmlattribute(attributename="name")]     public string name { get; set; }     [xmlelement(elementname="step")]     public step step { get; set; } }  [xmlroot(elementname="steps")] public class steps  {     [xmlelement(elementname="step")]     public step step { get; set; } } 

my xml :

<steps>   <step name="2.3  upper">     <step name="2.3.1 upper">       <comp ref="15.txt" dir="d:\test" />       <comp ref="16.txt" dir="d:\test2" />     </step>     <step name="2.3.2  upper" >       <comp ref="19.txt" dir="d:\test" />       <comp ref="29.txt" dir="d:\test2" />     </step>   </step> </steps> 

update: new xml file more nested step.

<steps>   <step name="2.3  upper">     <step name="2.3.1 upper">       <step name="2.3.1.1 upper">         <comp ref="10.txt" dir="d:\test" />       </step>       <comp ref="15.txt" dir="d:\test" />       <comp ref="16.txt" dir="d:\test2" />     </step>     <step name="2.3.2  upper" >       <comp ref="19.txt" dir="d:\test" />       <comp ref="29.txt" dir="d:\test2" />     </step>   </step> </steps> 

may ask how ride of following error:

'step': member names cannot same enclosing type

i stuck on error. appreciate helps.

it's because in step class:

[xmlroot(elementname="step")] public class step  {     [xmlelement(elementname="comp")]     public comp comp { get; set; }     [xmlattribute(attributename="name")]     public string name { get; set; }     [xmlelement(elementname="step")]     public step step { get; set; } // <-- here issue } 

you have step within step called step (inception or what?!).

edit:

better suggestion:

remove steps class entirely - not needed.

update step class, so:

[xmlroot(elementname="step")] public class step  {     [xmlelement(elementname="comp")]     public comp comp { get; set; }     [xmlattribute(attributename="name")]     public string name { get; set; }     [xmlelement(elementname="steps")]     public list<step> steps { get; set; } } 

additional edit:

your xml come out this:

<steps>     <step name="step 1">         <steps>             <!-- 3 substeps in step 1 -->             <step name="step 1 - 1st substep" ></step>             <step name="step 1 - 2nd substep" ></step>             <step name="step 1 - 3rd substep" >                 <steps>                     <!-- 3rd 1 has own substep -->                     <step name="step 1 - 3rd substep - 1st substep"/>                 </steps>             </step>         </steps>     </step>      <step name="step 2">         <steps>             <!-- step 2 has 1 substep -->             <step name="step 2 - 1st substep"/>         </steps>     </step>      <step name="step 3">         <!-- no substeps in here -->     </step> </steps> 

obviously other elements/attributes within step element include own, in example.

the main point thing that's changed in step class can have own list of steps called steps or substeps (or whatever meaningful name give).


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -