WPF - 使用 XMLTextReader 在 XML 文档中搜索值

发布于 2024-07-10 04:15:11 字数 2004 浏览 16 评论 0原文

好吧,另一个 WPF 问题,我想这只是一般的 .NET。 我有一个从 URL 检索的 xml 文档。

我想从文档中获取多个值(天气数据、位置、一些其他字符串)。

当我使用 XmlTextReader 时,我可以调用我的方法来提取值。 第一次传递该方法来搜索 xml 节点并获取值(XMLTextReader 对象)时,我得到了正确的数据,但随后 XMLTextReader 就死了。 不知道为什么它会被取消。 所以我必须在 FindTags... 方法中执行下面这个丑陋的代码。 我想继续将 xtr (XMLTextreader) 传递回我的 find 方法。 这是读者的本性吗? 我也不想每次都点击 URL...这似乎也是错误的。

帮助..这一切都感觉不对劲。

谢谢。

        GetWeatherFeed("97229", "//weather//loc//dnam", "//weather//cc//tmp", "/weather/cc/icon");

获取 WeatherFeed 方法(截图)

        System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(Url that retuns xm);
        System.Collections.Hashtable ht = new System.Collections.Hashtable();

        ht = FindTagsUsingXPthNaviatorAndXPathDocumentNew(xtr, location, temperature, iconid);
        lblLocation.Content = ht["Location"].ToString();
        lblWeatherCondition.Content = ht["Weather"].ToString();


public System.Collections.Hashtable FindTagsUsingXPthNaviatorAndXPathDocumentNew(System.Xml.XmlTextReader xtr, string nodeToLocate1, string nodeToLocate2, string nodeToLocate3)
{
    System.Xml.XPath.XPathDocument xpDoc = new System.Xml.XPath.XPathDocument(xtr);
    System.Xml.XPath.XPathNavigator xpNav = xpDoc.CreateNavigator();
    System.Xml.XPath.XPathExpression xpExpression = xpNav.Compile(nodeToLocate1);

    System.Xml.XPath.XPathNodeIterator xpIter = xpNav.Select(xpExpression);
    System.Collections.Hashtable ht = new System.Collections.Hashtable();

    while (xpIter.MoveNext())
    {
        ht.Add("Location", xpIter.Current.Value);
    }

    xpExpression = xpNav.Compile(nodeToLocate2);

    xpIter = xpNav.Select(xpExpression);
    while (xpIter.MoveNext())
    {
        ht.Add("Weather", xpIter.Current.Value);
    }

    xpExpression = xpNav.Compile(nodeToLocate3);

    xpIter = xpNav.Select(xpExpression);
    while (xpIter.MoveNext())
    {
        ht.Add("Icon", xpIter.Current.Value);
    }

    return ht;
}

Ok another WPF question, well I guess this is just general .NET. I have an xml document retreived from a URL.

I want to get multiple values out of the document (weather data, location, some other strings).

When I use the XmlTextReader I can call my method to pull the values out. The first time I pass the method to search for the xml node and get the value (the XMLTextReader object) I get the right data back, but then the XMLTextReader is dead. Not sure why it gets nulled out. So I'm having to do this UGLY code below in the FindTags... method. I want to just keep passing the xtr (XMLTextreader) back to my find method. Is this the nature of the reader? I don't want to have to hit the URL each time either... that seems wrong too.

Help.. this just all feels wrong.

Thanks.

        GetWeatherFeed("97229", "//weather//loc//dnam", "//weather//cc//tmp", "/weather/cc/icon");

Get WeatherFeed method (snipped)

        System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(Url that retuns xm);
        System.Collections.Hashtable ht = new System.Collections.Hashtable();

        ht = FindTagsUsingXPthNaviatorAndXPathDocumentNew(xtr, location, temperature, iconid);
        lblLocation.Content = ht["Location"].ToString();
        lblWeatherCondition.Content = ht["Weather"].ToString();


public System.Collections.Hashtable FindTagsUsingXPthNaviatorAndXPathDocumentNew(System.Xml.XmlTextReader xtr, string nodeToLocate1, string nodeToLocate2, string nodeToLocate3)
{
    System.Xml.XPath.XPathDocument xpDoc = new System.Xml.XPath.XPathDocument(xtr);
    System.Xml.XPath.XPathNavigator xpNav = xpDoc.CreateNavigator();
    System.Xml.XPath.XPathExpression xpExpression = xpNav.Compile(nodeToLocate1);

    System.Xml.XPath.XPathNodeIterator xpIter = xpNav.Select(xpExpression);
    System.Collections.Hashtable ht = new System.Collections.Hashtable();

    while (xpIter.MoveNext())
    {
        ht.Add("Location", xpIter.Current.Value);
    }

    xpExpression = xpNav.Compile(nodeToLocate2);

    xpIter = xpNav.Select(xpExpression);
    while (xpIter.MoveNext())
    {
        ht.Add("Weather", xpIter.Current.Value);
    }

    xpExpression = xpNav.Compile(nodeToLocate3);

    xpIter = xpNav.Select(xpExpression);
    while (xpIter.MoveNext())
    {
        ht.Add("Icon", xpIter.Current.Value);
    }

    return ht;
}

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

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

发布评论

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

评论(3

已下线请稍等 2024-07-17 04:15:11

这就是我所做的...很好的答案。

            System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(my xml url);
            System.Xml.XPath.XPathDocument xdoc = new System.Xml.XPath.XPathDocument(xtr);

            lblLocation.Content = getXmlNodeValue(xdoc, location);
            lblWeatherCondition.Content = getXmlNodeValue(xdoc, temperature);

Here's what I did... great answer.

            System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(my xml url);
            System.Xml.XPath.XPathDocument xdoc = new System.Xml.XPath.XPathDocument(xtr);

            lblLocation.Content = getXmlNodeValue(xdoc, location);
            lblWeatherCondition.Content = getXmlNodeValue(xdoc, temperature);
孤君无依 2024-07-17 04:15:11

XmlTextReader 无法重置为开头。
首先下载内容,然后使用多个 XmlTextReader(如果必须)。

如果您下载的文档很小,我只会使用 XmlDocument(如果您使用的是 .NET 3.5,则使用 XDocument)

XmlTextReader cannot be reset to the beginning.
Download you content first and then use multiple XmlTextReaders (if you have to).

If the document you are downloading is small, I would just use an XmlDocument (or XDocument if you are using .NET 3.5)

落在眉间の轻吻 2024-07-17 04:15:11

XMLTextReader 不是 SAX 阅读器吗? 您不需要倒带流才能再次读取文件吗?

Isn't XMLTextReader a SAX reader? Don't you have to rewind the stream to read the file in again?

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