【问题标题】:XmlNode in XmlNodelistXmlNodelist 中的 XmlNode
【发布时间】:2013-02-25 00:18:17
【问题描述】:

有谁知道错误在哪里?还是将视频名称转换为字符串的更好方法?

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

<?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>

【问题讨论】:

  • 您可以直接在标题之后使用 xpath = feed/entry/title 并在迭代中跳过查找,但您有什么问题?
  • 不明白你的意思。如果我运行代码,它不会显示消息框。
  • 这部分在您的问题中并不清楚。如果您非常具体地了解您希望您的代码做什么以及它正在做什么,它有助于生成答案

标签: c# xml xpath youtube namespaces


【解决方案1】:

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

        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);
        }

【讨论】:

  • 我一直在努力解决这个问题,找到了这个答案,它解决了我所有的问题。重要的是要了解为什么需要实施这是我的问题,现在我明白了。谢谢
【解决方案2】:

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

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);

【讨论】:

    【解决方案3】:

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

            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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多