Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

c# - Linq to XML with nested Dictionary and Array

I am Working on Windows Phone 8 application:

I have a document like this :

<array>
    <dict>
        <key>SubTopics</key>
        <array>
            <dict>
                <key>ID</key>
                <array>
                    <string>CD1</string>
                    <string>CD2</string>
                    <string>CD3</string>
                    <string>CD4</string>    
                </array>
                <key>Title</key>
                <string>Miscellaneous</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
            <dict>
                <key>ID</key>
                <array>
                    <string>DDC1</string>
                    <string>DDC2</string>
                    <string>DDC3</string>
                    <string>DDC4</string>
                    <string>DDC5</string>
                </array>
                <key>Title</key>
                <string>Miscellaneous One</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
      </array>
      <key>MainTitle</key>
      <string>Data</string>
    </dict>
    <dict>
        <key>SubTopics</key>
        <array>
            <dict>
                <key>ID</key>
                <array>
                    <string>SSD1</string>
                    <string>SS2</string>
                    <string>SS3</string>
                    <string>SS4</string>    
                </array>
                <key>Title</key>
                <string>Goblins</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
            <dict>
                <key>ID</key>
                <array>
                    <string>ADC1</string>
                    <string>ADC2</string>
                    <string>ADC3</string>
                    <string>ADC4</string>
                    <string>DDC5</string>
                </array>
                <key>Title</key>
                <string>Tracks</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
      </array>
      <key>MainTitle</key>
      <string>Data Two</string>
    </dict>
</array>

How to parse this ?

Its like this :

MainTitle 

   --SubTitle

  ---ID

  ---Desc

  ---Boolean Value
 MainTitle 

   --SubTitle

  ---ID

  ---Desc

  ---Boolean Value

Basically a List of Dictionary with Key and Array list of values.

I have tried like this but its not complete:

List<MyObject> topics = (from plist in doc.Root.Element("array").Elements("dict")
                                  select new MyObject
                                  {
                                      MainTitle = (string)plist.Element("string"),
                                      ListOfSubTitles = plist.Element("array")
                                                   .Elements("dict")
                                                   .Elements("string")
                                                   .Select(s => (string)s)
                                                   .ToList(),
                                      ListOfIDs = plist.Element("array")
                                                    .Elements("dict")
                                                    .Elements("array")
                                                    .Elements("string")
                                                    .Select(s => (string)s)
                                                    .ToList()
                                  }).ToList();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, here is a little class that can parse your xml:

public class Parser
{
    public List<Dictionary<string, object>> Parse(XElement root)
    {
        var result = new List<Dictionary<string, object>>();

        foreach (var e in root.Elements())
        {
            if (e.Name == "dict")
            {
                result.Add(ParseDict(e));
            }
        }

        return result;
    }

    private Dictionary<string, object> ParseDict(XElement element)
    {
        var dict = new Dictionary<string, object>();

        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<object>(valueElement.Elements().Select(e => ParseValue(e)));
        }

        if (valueElement.Name == "dict")
        {
            return ParseDict(valueElement);
        }

        if (valueElement.Name == "true")
        {
            return true;
        }

        if (valueElement.Name == "false")
        {
            return false;
        }

        return null;
    }
}

It is used like this:

        var parser = new Parser();
        var doc = XDocument.Load(<path to xml file>);

        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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...