【问题标题】:Parsing XML in Windows Phone 8在 Windows Phone 8 中解析 XML
【发布时间】:2015-10-18 20:45:14
【问题描述】:

我有一个如下所示的 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”的“section”的电台名称。 例如,我的结果将是“Kuraby Mosque,Gold Coast Mosque,...”。 我怎样才能达到结果??

N:B: 我可以使用以下代码在“部分”标签下显示名称(给出结果 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");
        }
    }

【问题讨论】:

  • 发布相关代码,展示如何在“section”标签下获取名称,以便我们可以从该代码开始

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


【解决方案1】:

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

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

Dotnetfiddle Demo

【讨论】:

  • 但是我只能得到第一个名字,即“Kuraby Mosque”,但我还需要所有的名字,比如“Gold Coast Mosque”等等。
  • 我应该学习什么才能知道这些问题。你能给我任何好的教程链接来学习这些解析吗?
  • 我怎样才能得到站“url”,“icon”呢??@har07
  • @imonbayazid 搜索“Linq to XML”参考/教程。祝你好运!
  • 再一次,我怎样才能得到站“url”,“icon”呢?? @har07
猜你喜欢
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 2014-05-05
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多