CF 2.0 XPATH problem

J

Jochen Mehlhorn

Hey,


I'm trying to use Xpath to "query" an XmlDocument, but I am not getting any
results.

My code looks like this:
using System.Xml;
using System.Xml.XPath;

XmlDocument xmlSourceDoc = new XmlDocument();
xmlSourceDoc.Load(path_to_xmlfile);
resultNodeList=xmlSourceDoc.SelectNodes( <<xpathStatement>> );


My XML looks something like this:

<Data>
<Entry att1="text" />
<Entry att1="text2" />
</Data>

Even simple xpath statements like "/Data/Entry", "//Entry" or "/Data" return
0 nodes.
I'm using a Windows Mobile 5.0, Compact Framework 2.0 emulator.

What am I doing wrong?


Thanks,

Jochen
 
S

Simon Hart

Are you using namespaces in your Xml (I know it's not included in your
sample), if so are you setting it during the XPath expression call?

Simon.
 
J

Jochen Mehlhorn

Hi,

that's a good point, I ommited that in my example because I thought it won't
be of any matter.

In fact I have specified a name space for my DocumentElement:

============================
<?xml version="1.0" encoding="utf-8" ?>
<Data xmlns="http://www.w3.org/2001/XMLSchema-instance"
SchemaLocation="some.xsd">
<Entry att1="text" />
<Entry att1="text2" />
</Data>
============================

In C# I now tried:

============================
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlSourceDoc.NameTable);
nsmgr. AddNamespace("", http://www.w3.org/2001/XMLSchema-instance);
XmlNodeList constrainedElements =
xmlSourceDoc.DocumentElement.SelectNodes("/Data/Entry", nsmgr);
============================

But unfortunately that didn't work out either.
Then I removed all namespacing information (the manager and the "xmlns=..."
and tried my first attempts again, but no luck.

I don't get what's going wrong here.

Jochen
 
J

Jochen Mehlhorn

Hey,

after looking into the namespace thing, I came across an article.

"How do you match elements in the default namespace (e.g. xmlns="uri") in
XPath 1.0?"
http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/791

This was the key to the solution.

It is not unusal not to explicitly use namespaces but a default one. Yet,
for some reason XPath 1.0 needs all elements to have a namespace prefix for
its matching process. At least in the Compact Framework that is.

So I changed the NameSpaceManager to:

============================
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlSourceDoc.NameTable);
nsmgr. AddNamespace("default", http://www.w3.org/2001/XMLSchema-instance);
============================

And then changed my XPath expression to "//default:Entry" and voilà, 2
matches.
Note that there were no changes to the XML neccessary.. this prefix is
only.. well, virtual.

What I don't get is: Why do I even need to bother with that stuff when
there's an method for SelectNodes() that accepts merely the xpath
expression, without NameSpaceManager.

Thanks for pointing me the right way, Simon!


Jochen
 
S

Simon Hart

What I don't get is: Why do I even need to bother with that stuff when
there's an method for SelectNodes() that accepts merely the xpath
expression, without NameSpaceManager.

Glad you got it working.

Because not all Xml uses namespaces. You could also use
GetElementsByTagName() which is supported under CF 1.0 and 2.0 as this will
return an array of nodes without the need to specify the namespace. But
caution should be taken when using this method for performance and event
wired reasons as it returns a "live collection" which is updated whenever
any changes to the document are made.

See here for these reasons:
http://blogs.msdn.com/eriksalt/archive/2005/07/20/GetElementsByTagName.aspx
http://support.microsoft.com/kb/823928/en-us
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top