c# - 在 Windows Phone 8 中解析 XML

标签 c# xml windows-phone-8 linq-to-xml

我有一个如下所示的 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>

我想显示所有具有名为“QLD Mosques”的“部分”的车站名称。 例如,我的结果将是“库拉比清真寺,黄金海岸清真寺,...”。 我怎样才能达到结果?

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

关于c# - 在 Windows Phone 8 中解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673864/

相关文章:

mysql - 如何从 Windows Phone 8 应用程序连接到远程 MySQL 数据库?

c# - 如果我在多个同时查询中使用 IsolationLevel.Snapshot 进行事务查询,SELECT @@IDENTITY 将返回什么?

c# - 在机器上的所有网站上包括 Javascript

javascript - 按值访问 XML 节点

android - 如何在 ConstraintLayout 中设置 View 的绝对位置

c# - Windows Phone 8,应用程序设置未保留

c# - 无法使用 HTMLAgilityPack 找到节点

c# - 基类如何从 Global.asax 调用 Session_End 方法?

c# - 从输入中删除 CDATA

适用于 Windows(桌面)和 Windows Phone 8 的 C# MVVM 应用程序框架