首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XmlNodelist中的XmlNode

XmlNodelist中的XmlNode
EN

Stack Overflow用户
提问于 2013-03-09 21:26:50
回答 3查看 9.5K关注 0票数 0

有人知道哪里出错了吗?或者是将视频名称转换为字符串的更好方法?

代码语言:javascript
复制
string text = "<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'><entry><title>VIDEO NAME</title></entry></feed>";
string xpath = "feed/entry";
XmlDocument xml = new XmlDocument();
xml.LoadXml(text);
XmlNodeList nodes = xml.SelectNodes(xpath);
foreach (XmlNode node in nodes)
{
    string title = node["title"].InnerText;
    MessageBox.Show(title);
}

XML

代码语言:javascript
复制
<?xml version='1.0' encoding='UTF-8'?>
    <feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
      <entry>
        <title>VIDEO NAME</title>
      </entry>
    </feed>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-09 21:47:03

Xml xmlns='http://www.w3.org/2005/Atom'中的这个声明将文档中所有没有名称空间前缀的元素放在默认名称空间http://www.w3.org/2005/Atom/中。因此,您需要在XPath查询中使用名称空间:

代码语言:javascript
复制
        string text = "<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'><entry><title>VIDEO NAME</title></entry></feed>";

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(text);
        XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(xml.NameTable);
        nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
        string xpath = "atom:feed/atom:entry/atom:title";
        XmlNodeList nodes = xml.SelectNodes(xpath, nsmgr);

        foreach (XmlNode node in nodes)
        {
            Console.WriteLine(node.InnerText);
        }
票数 3
EN

Stack Overflow用户

发布于 2013-03-09 21:53:06

您可以使用LINQ to XML代替XmlDocument和XPath:

代码语言:javascript
复制
string text = "<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'><entry><title>VIDEO NAME</title></entry></feed>";
var doc = XDocument.Parse(text);
var atom = XNamespace.Get("http://www.w3.org/2005/Atom");

var titles = doc.Descendants(atom + "entry")
                .Select(e => (string)e.Element(atom + "title"))
                .ToList();

foreach (string title in titles)
    Console.WriteLine(title);
票数 1
EN

Stack Overflow用户

发布于 2013-03-09 21:44:35

这是可行的,但我生成的代码感觉就像是一个肮脏的黑客

代码语言:javascript
复制
        string text = "<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'><entry><title>VIDEO NAME</title></entry></feed>";
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(text);
        XmlNode parentNode = xml.GetElementsByTagName("feed").Item(0);
        foreach (XmlNode n in parentNode.ChildNodes)
        {
            string title = n["title"].InnerText;
            Console.WriteLine(title);
        }

        Console.ReadLine();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15311194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档