将 XML 解析为对象数组 - 使用 Silverlight / Windows Phone

发布于 2024-12-01 03:55:00 字数 2114 浏览 0 评论 0原文

我通过 WebClient 方法调用 Restful 服务来返回一些 XML。然后我想解析 XML,从每个节点中提取特定字段,并将其转换为数组。

我有代码用于检索 XML 并将其填充到列表框中。由于某种原因,我无法弄清楚如何将其转换为对象数组。

到目前为止的代码:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += HttpsCompleted;
        wc.DownloadStringAsync(new Uri(requestString));
    }

    private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);

            var data = from query in xdoc.Descendants("entry")
                       select new DummyClass
                       {
                           Name = (string)query.Element("title"),
                           Kitty = (string)query.Element("countryCode")
                       };
            listBox1.ItemsSource = data;
        }
    }

}

如何将每个节点转换为数组中的对象?

非常感谢! 将要。

编辑:XML 如下所示: http://api.geonames.org/findNearbyWikipedia?lat=52.5469285&lng=13.413550&username=demo&radius=20&maxRows=5

<geonames>
<entry>
<lang>en</lang>
<title>Berlin Schönhauser Allee station</title>
<summary>
Berlin Schönhauser Allee is a railway station in the Prenzlauer Berg district of Berlin. It is located on the Berlin U-Bahn line and also on the Ringbahn (Berlin S-Bahn). Build in 1913 by A.Grenander opened as "Bahnhof Nordring" (...)
</summary>
<feature/>
<countryCode>DE</countryCode>
<elevation>54</elevation>
<lat>52.5494</lat>
<lng>13.4139</lng>
<wikipediaUrl>
http://en.wikipedia.org/wiki/Berlin_Sch%C3%B6nhauser_Allee_station
</wikipediaUrl>
<thumbnailImg/>
<rank>93</rank>
<distance>0.2807</distance>
</entry>
</geonames>

I am calling a restful service via the WebClient method to return some XML. I would then like to parse through the XML, extract specific fields out of each node, and turn that into an array.

I have the code working to retrieve the XML and populate it into a listbox. For some reason I cannot work out how to turn that into an array of objects.

Code so far:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += HttpsCompleted;
        wc.DownloadStringAsync(new Uri(requestString));
    }

    private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);

            var data = from query in xdoc.Descendants("entry")
                       select new DummyClass
                       {
                           Name = (string)query.Element("title"),
                           Kitty = (string)query.Element("countryCode")
                       };
            listBox1.ItemsSource = data;
        }
    }

}

How can I turn each node into an object in an array?

Many thanks in advance!
Will.

EDIT: The XML looks like this:
http://api.geonames.org/findNearbyWikipedia?lat=52.5469285&lng=13.413550&username=demo&radius=20&maxRows=5

<geonames>
<entry>
<lang>en</lang>
<title>Berlin Schönhauser Allee station</title>
<summary>
Berlin Schönhauser Allee is a railway station in the Prenzlauer Berg district of Berlin. It is located on the Berlin U-Bahn line and also on the Ringbahn (Berlin S-Bahn). Build in 1913 by A.Grenander opened as "Bahnhof Nordring" (...)
</summary>
<feature/>
<countryCode>DE</countryCode>
<elevation>54</elevation>
<lat>52.5494</lat>
<lng>13.4139</lng>
<wikipediaUrl>
http://en.wikipedia.org/wiki/Berlin_Sch%C3%B6nhauser_Allee_station
</wikipediaUrl>
<thumbnailImg/>
<rank>93</rank>
<distance>0.2807</distance>
</entry>
</geonames>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2024-12-08 03:55:00

有什么问题

// convert IEnumerable linq query to an array
var array = data.ToArray(); // could also use .ToList() for a list
// access like this
MessageBox.Show(array[0].Kitty);

这将为您提供一个由 linq 查询生成的 IEnumerable对象组成的 DummyClass 对象数组。

此外,甚至可能不需要数组。如果您需要做的只是迭代数据,则只需在 data 对象上执行 foreach 即可。

What is wrong with

// convert IEnumerable linq query to an array
var array = data.ToArray(); // could also use .ToList() for a list
// access like this
MessageBox.Show(array[0].Kitty);

This will give you an array of DummyClass objects, from the IEnumerable<DummyClass> generated by the linq query.

Additionally, an array may not even be required. If all you need to do is iterate over the data, you can simply do a foreach on your data object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文