I am Working on Windows Phone 8 application:
I have a document like this :
SubTopics
Ok, here is a little class that can parse your xml:
public class Parser
{
public List> Parse(XElement root)
{
var result = new List>();
foreach (var e in root.Elements())
{
if (e.Name == "dict")
{
result.Add(ParseDict(e));
}
}
return result;
}
private Dictionary ParseDict(XElement element)
{
var dict = new Dictionary();
foreach (var subelement in element.Elements())
{
if (subelement.Name == "key")
{
dict.Add(subelement.Value, ParseValue(subelement.ElementsAfterSelf().First()));
}
}
return dict;
}
private object ParseValue(XElement valueElement)
{
if (valueElement.Name == "string")
{
return valueElement.Value;
}
if (valueElement.Name == "array")
{
return new List
It is used like this:
var parser = new Parser();
var doc = XDocument.Load();
var result = parser.Parse(doc.Root);
The parser is very crude and makes assumptions about the xml. And as pointed out in earlier comments, it is not the best way to use xml like this, where position of elements has significance. Also the use of "object" in the parser isn't a good solution, but the parser gets a lot more advanced if you want to avoid that.