简体   繁体   中英

Get all descendants LINQ to XML

I have this XML format but have removed the bulk of it as this is the only information I need to extract. The parts I extracted are named similarly, so there exist other elements named dict, key, array and string - ie just pulling values from the string elements isn't an option.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>CFBundleIcons</key>
        <dict>
            <key>CFBundlePrimaryIcon</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>AppIcon29x29</string>
                    <string>AppIcon40x40</string>
                    <string>AppIcon60x60</string>
                </array>
            </dict>
        </dict>
        <key>CFBundleIcons~ipad</key>
        <dict>
            <key>CFBundlePrimaryIcon</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>AppIcon29x29</string>
                    <string>AppIcon40x40</string>
                    <string>AppIcon60x60</string>
                    <string>AppIcon76x76</string>
                </array>
            </dict>
        </dict>
    </dict>
</plist>

The closest I've come is this:

XElement doc = XElement.Load(outputFolder + "\\Info.xml");
IEnumerable<XElement> output = doc.Descendants("key").Where(n => (string)n.Value == "CFBundleIconFiles");
foreach (XElement a in output)
    MessageBox.Show((a.NextNode as XElement).Value);

This makes two alerts appear, the first says: "AppIcon29x29AppIcon40x40AppIcon60x60" for the first one and the second says "AppIcon29x29AppIcon40x40AppIcon60x60AppIcon76x76" which is annoying because I'm so close yet so far. I also feel like I'm doing this in an awful way that will make a few of you cringe.

Thanks in advance!

Edit: I want the strings in the CFBundleIconFiles array.

It's simple:

IEnumerable<XElement> output = doc.Descendants("key")
  .Where(n => n.Value == "CFBundleIconFiles");

IEnumerable<string> result = 
  output.SelectMany(a => 
    (a.NextNode as XElement).Descendants().Select(n => n.Value));

MessageBox.Show(string.Join(Environment.NewLine, result));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM