将 XML 解析为对象数组 - 使用 Silverlight / Windows Phone
我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有什么问题
这将为您提供一个由 linq 查询生成的 IEnumerable对象组成的
DummyClass
对象数组。此外,甚至可能不需要数组。如果您需要做的只是迭代数据,则只需在
data
对象上执行foreach
即可。What is wrong with
This will give you an array of
DummyClass
objects, from theIEnumerable<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 yourdata
object.