XmlNode in XmlNodelist

本文关键字:XmlNodelist in XmlNode | 更新日期: 2023-09-27 18:03:11

谁知道错误在哪里?或者是更好的方式得到视频名称成字符串?

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>

XmlNode in XmlNodelist

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

您可以使用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);

这可以工作,但是我产生的代码感觉像这样一个肮脏的hack

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