1

I have an XML like this:

<roottag>
    <tag1>.....</tag1>
    <tag2>.....</tag2>
    <tag3>
        <tag3.1>.....</tag3.1>
        <tag3.2>.....</tag3.2>
   </tag3>
    <tag4>
        <tag4.1>
            <tag4.1.1>......</tag4.1.1>
        </tag4.1>
   </tag4>
</roottag>

I want to convert this XML into a form which can be easily traversed and also maintain the the hierarchy in the XML. In php, there is Nested Associative Array which can be used. In C# How can I accomplish this ?

Mayank
  • 1,099
  • 4
  • 17
  • 44
  • 2
    `XDocument xDoc = XDocument.Parse("xmlstring");` You can then use LINQ to XML to traverse/navigate the XML. – Tim Sep 02 '13 at 09:47
  • You also may use `XSLT` – MikkaRin Sep 02 '13 at 09:51
  • You can deserialize the XML into object.[This](http://stackoverflow.com/questions/14722492/xml-string-deserialization-into-c-sharp-object) may help you. – Naren Sep 02 '13 at 09:54
  • I basically want to convert it into some sort of `Associative Array`. Here in C#, I guess I can use `Dictionary`, but can I use the nested `Dictionary` to maintain the hierarchy also ? – Mayank Sep 02 '13 at 10:01
  • @Mayank Nested dictionary is a good option.Try [this](http://stackoverflow.com/questions/12543094/nested-dictionary-collection-in-net) link.But you have to declare the nesting first. – Naren Sep 02 '13 at 10:16
  • The hierarchy of XML gets changed from time to time. I cannot declare the nesting first, it has to be at runtime. – Mayank Sep 02 '13 at 10:30
  • Why can't you use a List to do this? – Vimal CK Sep 02 '13 at 10:57
  • @VimalCK....thanks for the suggestion, but I am not aware about how to use the `List`. Can you please enlighten me on this. – Mayank Sep 02 '13 at 11:00

0 Answers0