有谁知道哪里出错了?或者是将视频名称转换为字符串的更好方法?
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 :(得分:3)
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);
}
答案 1 :(得分:1)
您可以使用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);
答案 2 :(得分: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();