c#
  • xml
  • xpath
  • youtube
  • namespaces
  • 2013-03-09 63 views -1 likes 
    -1

    有谁知道哪里出错?或者是将视频名称转换为字符串的更好方法?XmlNodelist中的XmlNode

    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> 
    
    +0

    您可以在标题使用XPath =饲料/入境后右转/标题并在迭代中跳过查找,但你有什么问题? – dkackman 2013-03-09 13:40:21

    +0

    不明白你的意思。如果我运行代码,它不显示消息框。 – Bullman 2013-03-09 13:50:23

    +2

    该部分在您的问题中不清楚。如果你对你期望的代码做什么以及它在做什么非常具体,它有助于产生答案 – dkackman 2013-03-09 13:51:48

    回答

    2

    这个声明在XML xmlns='http://www.w3.org/2005/Atom'把所有的元素在文档中,唐” t在默认名称空间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); 
         } 
    
    +0

    谢谢!它帮助 – Bullman 2013-03-09 14:08:21

    +1

    我正在努力解决这个问题,发现了这个答案,它解决了我所有的问题。重要的是要理解为什么需要实现这个是我的问题,现在我明白了。谢谢 – 2013-09-02 02:49:46

    0

    这工作,但我的代码产生像这样一个肮脏的黑客感觉

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

    您可以使用LINQ到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); 
    
    相关问题