在 Windows Phone 中解析 XML 8

Parsing XML in Windows Phone 8

我有一个 xml 文件,如下所示。

<sections>
   <section>
      <name>QLD Mosques</name>
      <stations>
         <station>
            <id>101</id>
            <pn>true</pn>
            <name>Kuraby Mosque</name>
            <url>http://sb2110.ultrastream.co.uk/kuraby</url>
            <icon>...</icon>
         </station>
         <station>
            <id>102</id>
            <pn>true</pn>
            <name>Gold Coast Mosque</name>
            <url>http://sb2110.ultrastream.co.uk/goldcoast</url>
            <icon>http://www.juju.net.au/mosquereceivers/images/icons/gc.jpg</icon>
         </station>
         <station>...</station>
      </stations>
   </section>
   <section>
      <name>NZ Mosques</name>
      <stations>...</stations>
   </section>
   <section>
      <name>Islamic Radio Stations</name>
      <stations>...</stations>
   </section>
</sections>

我想显示所有 "section" 名为 "QLD Mosques" 的电台名称。 例如,我的结果将是 "Kuraby Mosque,Gold Coast Mosque,..."。 我怎样才能达到这个结果??

N:B: 我可以使用以下代码显示 "section" 标签下的名称(结果为 QLD Mosques、NZ Mosques、Islamic Radio Stations):

public static List<MyData> channel_main_list = new List<MyData>();

    public MainChannelList()
    {
        InitializeComponent();


        WebClient client = new WebClient();
        client.OpenReadCompleted += client_OpenReadCompleted;
        client.OpenReadAsync(new Uri("http://www.juju.net.au/mosquereceivers/Stations.xml",UriKind.Absolute));

    }

    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        Stream str = e.Result;

        string node = "section";

        XDocument loadedData = XDocument.Load(str);

        try
        {
            foreach (var item in loadedData.Descendants(node))
            {

                try
                {
                    MyData m = new MyData();
                    m.channel_name = item.Element("name").Value;

                    channel_main_list.Add(m);
                }
                catch (Exception)
                {

                    MessageBox.Show("Problem");
                }



            }

            listBox.ItemsSource = channel_main_list;

        }
        catch (Exception)
        {
            MessageBox.Show("Connectivity Problem");
        }
    }

这是一种可能的方式,假设 XML 结构是一致的,并且总能找到正在搜索的 name :

var result = loadedData.Descendants("section")
                       .Where(o => (string)o.Element("name") == "QLD Mosques")
                       .Elements("stations")
                       .Elements("station")
                       .Elements("name");

Dotnetfiddle Demo