c#
  • xml
  • xpath
  • youtube
  • namespaces
  • 2013-03-09 55 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); 
    
    相關問題