1
Answer

c# Serialise Dynamic XML into dictionary

Photo of James Lerdorf

James Lerdorf

13y
2.8k
1
HI,

I have some XML; <data> <name1>Ramesh</name1> <name2>Kumar</name2> etc.. </data> The name1, name2, etc could be anything,
i want to be able to get this xml.


Thanks

Answers (1)

0
Photo of Vulpes
NA 98.3k 1.5m 13y
Try this:

    using System.Xml.Linq;

    // ....

    string xml = "<data> <name1>Ramesh</name1> <name2>Kumar</name2> </data>";
    XElement data = XElement.Parse(xml);
    var dict = new Dictionary<string, string>();
    foreach (XElement child in data.Elements()) dict.Add(child.Name.ToString(), child.Value);

    // check it worked
    string temp = "";
    foreach (string key in dict.Keys) temp += String.Format("{0} : {1}\r\n", key, dict[key]);
    MessageBox.Show(temp);
Next Recommended Forum