【问题标题】:Parsing through XML elements in XmlReader在 XmlReader 中解析 XML 元素
【发布时间】:2008-10-28 21:08:27
【问题描述】:

我正在构建一个需要通过 XML 提要运行的应用程序,但在获取某些元素时遇到了一点麻烦。

我正在使用Twitter feed 并希望遍历所有<item> 元素。我可以很好地连接并从提要中获取内容,但是当我循环访问reader.Read(); 时,我无法弄清楚如何只选择item 元素。

感谢您的帮助!

【问题讨论】:

  • 您选择 XmlReader 而不是将 XML 加载到文档中是否有原因?什么框架版本?

标签: c# xmlreader


【解决方案1】:

最简单的方法是使用 XPath。要遵循的示例。

 string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<rss version=""2.0"">
    <channel>
    <title>Twitter public timeline</title>
    <link>http://twitter.com/public_timeline</link>
    <description>Twitter updates from everyone!</description>
    <language>en-us</language>
    <ttl>40</ttl>

    <item>
      <title>yasu_kobayashi: rTwT: @junm : yayaya</title>
      <description>yasu_kobayashi: rTwT: @junm : yayaya</description>
      <pubDate>Tue, 28 Oct 2008 12:04:48 +0000</pubDate>
      <guid>http://twitter.com/yasu_kobayashi/statuses/978829930</guid>
      <link>http://twitter.com/yasu_kobayashi/statuses/978829930</link>

    </item><item>
      <title>FreeGroup: WikiFortio - foobar http://tinyurl.com/5gvttf</title>
      <description>FreeGroup: WikiFortio - foobar
      http://tinyurl.com/5gvttf</description>
      <pubDate>Tue, 28 Oct 2008 12:04:47 +0000</pubDate>
      <guid>http://twitter.com/FreeGroup/statuses/978829929</guid>
      <link>http://twitter.com/FreeGroup/statuses/978829929</link>

    </item></channel></rss>
        ";
            XPathDocument doc = new XPathDocument(new StringReader(xml));
            XPathNavigator nav = doc.CreateNavigator();

            // Compile a standard XPath expression

            XPathExpression expr;
            expr = nav.Compile("/rss/channel/item");
            XPathNodeIterator iterator = nav.Select(expr);

            // Iterate on the node set

            try
            {
                while (iterator.MoveNext())
                {
                    XPathNavigator nav2 = iterator.Current.Clone();
                    nav2.MoveToChild("title","");
                    Console.WriteLine(nav2.Value);
                    nav2.MoveToParent();
                    nav2.MoveToChild("pubDate","");
                    Console.WriteLine(nav2.Value);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();

这是 jan 的工作方式

        XmlDocument doc2 = new XmlDocument();
        doc2.LoadXml(xml);
        XmlNode root = doc2.DocumentElement;

        foreach (XmlNode item in root.SelectNodes(@"/rss/channel/item"))
        {
            Console.WriteLine(item.SelectSingleNode("title").FirstChild.Value);
            Console.WriteLine(item.SelectSingleNode("pubDate").FirstChild.Value);
        }

【讨论】:

    【解决方案2】:

    另一种选择:

    // starts as in Vinko Vrsalovic 's answer
    // and not including decent eror handling
    XmlDocument doc = new XmlDocument(new StringReader(xml)); 
    
    foreach (XmlNode item in doc.SelectNodes(@"/rss/channel/item"))
    {
      Console.WriteLine(item.SelectSingleNode("title").Value);
      Console.WriteLine(item.SelectSingleNode("pubDate").Value);
    }
    

    我不知道这段代码是较慢还是不好的做法。 请发表评论。

    我发现它比使用 Navigator 和 Iterator 的另一个更具可读性。

    编辑:我使用Xml文档。 Vinko Vrsalovic 的回答中的 XPathDocument 不支持这种工作方式,但速度要快得多: (MSDN)

    【讨论】:

    • 除非您有一个非常大的流,这使得将内容加载到文档中是不可取的,否则这将是一种合理的方法,并且比使用 XmlReader 更具可读性。
    • 我也喜欢这种方法。但是你不能将它与 XPathDocument 一起使用,你需要一个 XmlNode
    • 我的错!我的意思是 XmlDocument。但由于我可以想象 Twitter RSS 提要会变得非常大,所以我会去 - 在这种情况下 - 也使用 XPathDocument 来实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    相关资源
    最近更新 更多