Reputation: 328
Does anyone know where is mistake? Or is better way to get video name into string?
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>
Upvotes: 0
Views: 9814
Reputation: 15579
This declaration in the Xml xmlns='http://www.w3.org/2005/Atom'
puts all elements in the document that don't have a namespace prefix in the default namespace http://www.w3.org/2005/Atom/
. Therefore you need to use namespaces in your XPath queries:
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);
}
Upvotes: 3
Reputation: 125650
You can use LINQ to XML instead of XmlDocument and 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);
Upvotes: 1
Reputation: 16199
This works but the code I produced feels like such a dirty 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();
Upvotes: 0