-2
<root>
    <child id="1">
       <child1 id="1"/>
       <child2>
           <child11 id="1">
               <child111 id="1"/>
           </child11>
    </child2>
   </child>
    <child id="2">
       <child1 id="2"/>
    </child>
</root>

how can i get the exact above structure using deserialization

  • 1
    Welcome to stackoverflow. Please take a minute to take the [tour], especially [ask], and [edit] your question accordingly. – jazb Oct 04 '19 at 05:46

1 Answers1

1

You can roughly formalize the entity above as such:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace WorkingProject
{
    [XmlRoot(ElementName="child1")]
    public class Child1 {
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName="child111")]
    public class Child111 {
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName="child11")]
    public class Child11 {
        [XmlElement(ElementName="child111")]
        public Child111 Child111 { get; set; }
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName="child2")]
    public class Child2 {
        [XmlElement(ElementName="child11")]
        public Child11 Child11 { get; set; }
    }

    [XmlRoot(ElementName="child")]
    public class Child {
        [XmlElement(ElementName="child1")]
        public Child1 Child1 { get; set; }
        [XmlElement(ElementName="child2")]
        public Child2 Child2 { get; set; }
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName="root")]
    public class Root {
        [XmlElement(ElementName="child")]
        public List<Child> Child { get; set; }
    }

}

You can then use the XmlSerializer class to transform the XML string into a Root class instance (you can amend the names, where necessary).

Den
  • 16,686
  • 4
  • 47
  • 87