How to deserialize Xml file with nested elements of same name?

Mohamed Ahmed

I have XML file with this elements:

<Reqs>
    <Source>
        <Sec name="A">
            <Sec name="L">
                <Sec name="B">
                    <Req>
                        <Content></Content>
                        <Title></Title>
                        <Pro></Pro>
                    </Req>
               </Sec>
           </Sec>
      </Sec>
  </Source>
</Reqs>

The elements have the same Name and nested, How I can deserialize it using C# ?

Marc Gravell
public class Reqs {
    public Source Source {get;set;}    
}
public class Source {
    public Sec Sec {get;set;}
}
public class Sec {
    [XmlAttribute("name")]
    public string Name {get;set;}
    [XmlElement("Sec")]
    public Sec InnerSec { get; set; }

    public Req Req {get;set;}
}
public class Req {
    public string Content {get;set;}
    public string Title {get;set;}
    public string Pro {get;set;}
}

with:

var reqs = new Reqs {
    Source = new Source {
        Sec = new Sec {
            Name = "A",
            InnerSec = new Sec {
                Name = "L",
                InnerSec = new Sec {
                    Name = "B",
                    Req = new Req {
                        Content = "",
                        Title = "",
                        Pro = ""
                    }
                }
            }
        }
    }
};
var ser = new XmlSerializer(typeof(Reqs));
ser.Serialize(Console.Out, reqs);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I deserialize XML that has 2 elements with the same name but the elements have different datatypes?

Querying nested elements with same name in XML with LINQ

Deserialize different xml elements with same name into different C# classes

Deserialize xml Elements with same Attributes

How to deserialize same XML Element with different namespace into different elements in a struct

How to parse XML with nested nodes of the same name?

XSLT transformation XML-to-CSV accessing nested elements with same name

how to step through xml elements with the same name

using SAX parser, how do you parse an xml file which has same name tags but in different elements?

Jackson deserialize xml fields with the same name

Deserialize XML to derived class with same element name?

How to select elements with the same name from nested list with purrr?

How to unmarshal nested child elements in java with same tag name?

How to reach the nested child elements of same name and formats

Deserialize a xml file with child elements in c#

How to deserialize XML file with Array?

How to deserialize nested JSON object that have the same name as my Java class

How to access nested children with same name in XML using lxml in python

How to parse nested xml tags with the same tag name

Parse XML File with nested Elements

How to parse xml with child elements with the same name as parent

How to deserialize XML elements with variable number of children?

How to deserialize object to new object with the same name?

Deserialize XML elements with different type name in C#

DataContract deserialize XML - List of elements and properties in the same element

How to deserialize a JSON file ( using Google JSON) consisting of same key name but uses different type?

Serialize and deserialize Jackson XML element having the same name

Deserialize Xml Using Same Object With Different Element Name

Golang parse XML with nested nodes of the same name?