【发布时间】: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