从c#中的rss feed中提取数据

发布于 2024-08-16 00:22:43 字数 1145 浏览 3 评论 0原文

我需要从下面的 xml 文档中读取 woeid。我需要读取数据并将其存储到字符串变量中,以便可以查询雅虎天气服务。

查询返回的 XML:

<query yahoo:count="1"
       yahoo:created="2009-12-22T08:30:31Z"
       yahoo:lang="en-US"
       yahoo:updated="2009-12-22T08:30:31Z"
       yahoo:uri="http://query.yahooapis.com/v1/yql?q=select+woeid+from+geo.places+where+text%3D%22farnborough%2C+greater+london%2Cuk%22">
−
<diagnostics>
<publiclyCallable>true</publiclyCallable>
−
<url execution-time="32">
http://where.yahooapis.com/v1/places.q(farnborough%2C%20greater%20london%2Cuk);start=0;count=10
</url>
<user-time>33</user-time>
<service-time>32</service-time>
<build-version>4265</build-version>
</diagnostics>
−
<results>
−
<place>
<woeid>19941</woeid>
</place>
</results>
</query>

有人可以告诉我如何通过 linq 执行此操作吗?

- - - - - 编辑 - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- -

我刚刚意识到 .net 2.0 不支持 linq...doh

所以请有人建议使用 .net 2.0 提供的参考资料的替代方法吗? -也许重新发布并标记? 非常感谢,

I need to read the woeid from the xml doc below. I need to read and store the data into a string variable so I can query the yahoo weather service.

XML returned by query:

<query yahoo:count="1"
       yahoo:created="2009-12-22T08:30:31Z"
       yahoo:lang="en-US"
       yahoo:updated="2009-12-22T08:30:31Z"
       yahoo:uri="http://query.yahooapis.com/v1/yql?q=select+woeid+from+geo.places+where+text%3D%22farnborough%2C+greater+london%2Cuk%22">
−
<diagnostics>
<publiclyCallable>true</publiclyCallable>
−
<url execution-time="32">
http://where.yahooapis.com/v1/places.q(farnborough%2C%20greater%20london%2Cuk);start=0;count=10
</url>
<user-time>33</user-time>
<service-time>32</service-time>
<build-version>4265</build-version>
</diagnostics>
−
<results>
−
<place>
<woeid>19941</woeid>
</place>
</results>
</query>

Can someone show me how to do this through linq?

----------EDIT ------------------------------------------------------------------------------------------

I've just realised i linq is not supported by .net 2.0...doh

So please could some suggest an alternative way using references available with .net 2.0? -maybe repost and tag?
Many Thanks,

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

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

发布评论

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

评论(3

安静 2024-08-23 00:22:43

你可以这样做:

XDocument doc = XDocument.Parse(xml);
string s = doc.Descendants()
              .Where(element => element.Name == "woeid")
              .FirstOrDefault().Value;

You can do it like this:

XDocument doc = XDocument.Parse(xml);
string s = doc.Descendants()
              .Where(element => element.Name == "woeid")
              .FirstOrDefault().Value;
百合的盛世恋 2024-08-23 00:22:43

您可以使用类似于此 Linq 查询的方法从 XML 文档中获取结果

XDocument feeds = XDocument.Parse(xml);
var result = feeds.Descendants("diagnostics")
                    .Select(f => new 
                    {
                        UserTime = f.Element("uset-time").Value,
                        ServiceTime = f.Element("service-time").Value,
                        //... etc
                    }.First();

You can use something similar to this Linq query to get the results back from the XML document

XDocument feeds = XDocument.Parse(xml);
var result = feeds.Descendants("diagnostics")
                    .Select(f => new 
                    {
                        UserTime = f.Element("uset-time").Value,
                        ServiceTime = f.Element("service-time").Value,
                        //... etc
                    }.First();
嘿咻 2024-08-23 00:22:43

Here is a way to extract values from an xml file using Linq and xPath.

Hope this helps some.

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