【问题标题】:.net 2.0 c# retrieving a value from xml.net 2.0 c# 从 xml 检索值
【发布时间】:2009-12-23 10:38:26
【问题描述】:

我意识到这是一个转贴,但我需要,因为我不能使用 LINQ,(在我之前的帖子中要求)。

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

查询返回的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>

有人可以建议我如何通过 XPath 或与 .net 2.0 中的库兼容的其他方式来做到这一点吗?

感谢您的回答。因此,我使用下面的方法,但是在加载 XML 文档时抛出异常:

private string getWOEID(string UserLocation, string UserCountry)
{
    string woeID = "";

    if (UserLocation == "farnborough") { UserLocation = "farnborough" + "%2C" + "hampshire"; }

    String reqUrl = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text%3D%22" + UserLocation + "%2C" + UserCountry + "%22&format=xml";

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(reqUrl);
   string woeid = doc.SelectSingleNode("query/results/place/woeid/text()").Value;
    return woeID;


}

【问题讨论】:

  • 查看我的更新答案。
  • 感谢 ChaosPandion 我用修复重建。但是还是在doc.LoadXml(reqUrl)语句中抛出异常:{"根级别的数据无效。第1行,位置1。"}
  • 我更新了我的答案以满足您的需求。我将 LoadXml 切换为 Load。
  • 感谢混沌。它的工作几乎除了一个空值使用返回: string woeid = doc.SelectSingleNode("query/results/place/woeid/text()").Value;我也试过: string woeid = doc.SelectSingleNode("query/results/place/woeid/text()").InnerText;但不是快乐!
  • 我最终使用了 Mark 的方式 string woeId = doc.GetElementsByTagName("woeid")[0].InnerText;这对我有用。

标签: c# .net xml xpath .net-2.0


【解决方案1】:

常规方式:

XmlDocument doc = new XmlDocument();
doc.Load(url);
string woeid = doc.SelectSingleNode("query/place/woeid/text()").Value;

一种不太传统的做法:

int start = xmlString.IndexOf("<woeid>") + 7;
int end = xmlString.IndexOf("</woeid>", start);
string woeid = xmlString.Substring(start, end - start);

有人想运行基准测试吗?

【讨论】:

  • 我只是提供了 2 个选项,就像 Mark 提供了第 3 个选项一样。我看不出这值得投反对票。
  • 第一个例子不起作用,需要修复Xpath:string woeId = doc.SelectSingleNode("query/results/place/woeid/text()").Value;
  • 提供一个糟糕的选择值得一票。在 XML 上使用字符串操作会忽略特殊字符的处理以及一般的 XML 规则,并且几乎肯定会导致 XML 新手搞砸。
  • 感谢巴达罗的提醒。我很少使用文本节点,但在这种情况下效果很好。
  • 约翰,我注意到你有将近 600 票反对,所以我想你可能会有点高兴。我清楚地标记了更传统的问题,并提供了另一个(对于一个快速而肮脏的应用程序来说已经足够了)选项来让事情变得有趣。
【解决方案2】:

我将对此进行另一次尝试,以 ChaosPandion 的帖子为起点:

    XmlDocument doc = new XmlDocument();
    using (XmlTextReader tr = new XmlTextReader(new StringReader(xmlString)))
    {
        tr.Namespaces = false;
        doc.Load(tr);
    }
    string woeId = doc.GetElementsByTagName("woeid")[0].InnerText;

请注意,我在这里故意忽略命名空间,因为您不需要它们来做您想做的事情。

【讨论】:

  • 在 XPath 查询中使用 GetElementsByTagName 时有哪些性能注意事项?
  • -1 用于使用 XmlTextReader,已弃用。请改用XmlReader.Create
  • @John:不过,他只是用它来忽略命名空间。有没有办法用 XmlReader.Create 做到这一点?
  • 忽略命名空间是个坏主意。而是学习理解它们。
  • 请约翰告诉我们为什么忽略你不关心的事情是不好的。
猜你喜欢
  • 1970-01-01
  • 2018-02-15
  • 2013-05-22
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-24
相关资源
最近更新 更多