What am I doing wrong with this XPath query? Within the feed tag, there are multiple entry tags. The XML is valid, and so is the generated XPath query, I have tested both together with a tool called XPath Visualizer.
THe call to iterator.MoveNext() returns false, and after the operation the iterator is still on the root node. Am I missing something obvious here?
var stringReader = new StringReader(importData.Trim());
var xpathDoc = new XPathDocument(stringReader);
XPathNavigator xPathNav = xpathDoc.CreateNavigator();
string xPathString = "//" + masterNode; // Evaluates to '//entry'
XPathExpression xPathExpr = xPathNav.Compile(xPathString);
XPathNodeIterator iterator = xPathNav.Select(xPathExpr);
while (iterator.MoveNext())
{
....
Example XML
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
<title>Test</title>
<link rel="self" href="http://store.Test.com"/>
<updated>2012-02-07T11:15:04Z</updated>
<author>
<name>Test</name>
</author>
<id>tag:000000</id>
<entry>
<title><![CDATA[Example 7500cl]]></title>
<link><![CDATA[http://store.test.com/example-75cl/]]></link>
<description><![CDATA[example descr........]]></description>
</entry>
The problem is with namespaces – there aren’t any
entryelements without a namespace – the element you can see has the “http://www.w3.org/2005/Atom” namespace inherited from the root.Personally I’d use LINQ to XML for this instead, but if you must use XPath, you’ll need to use a namespace manager to find elements with a particular namespace. A search for “xpath namespace .net” within Stack Overflow will give you lots of hits with options.
The LINQ to XML code would look like this: