problem with XmlDocument.SelectNodes()

J

Jeff Dege

I have a simple GML file I'm trying to parse using
XmlDocument.SelectNodes, and it's not working.

The GML:

<wfs:FeatureCollection xmlns:wfs="http://www.opengis.net/wfs">
<gml:featureMember
xmlns:gml="http://www.opengis.net/gml">
<feature:features
xmlns:feature="http://mapserver.gis.umn.edu/mapserver">
<feature:geometry>
[...]
</feature:geometry>
</feature:features>
</gml:featureMember>
</wfs:FeatureCollection>

My code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(...);

XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("wfs", "http://www.opengis.net/wfs/");
nsm.AddNamespace("gml", "http://www.opengis.net/gml/");
nsm.AddNamespace("feature", "http://mapserver.gis.umn/edu/mapserver/");

XmlNodeList featureList = xmlDoc.SelectNodes("//feature:features", nsm);

The problem? featureList is empty - has zero nodes.

It's not that I don't have the namespaces loaded - when I had that wrong,
I got errors. I'm not getting errors. And I don't think I have the
xpath wrong - when I view the XML in FireFox, and bring up the XPath
Checker, an xpath of "//feature:features" highlights exactly the node I'd
have expected it to.

Any ideas?


--
Only justice, and not safety, is consistent with liberty, because safety
can be secured only by prior restraint and punishment of the innocent,
while justice begins with liberty and the concomitant presumption of
innocence and imposes punishment only after the fact.
- Jeffrey Snyder
 
J

Jon Skeet [C# MVP]

Jeff Dege said:
I have a simple GML file I'm trying to parse using
XmlDocument.SelectNodes, and it's not working.

The GML:

<wfs:FeatureCollection xmlns:wfs="http://www.opengis.net/wfs">
<gml:featureMember
xmlns:gml="http://www.opengis.net/gml">
<feature:features
xmlns:feature="http://mapserver.gis.umn.edu/mapserver">
<feature:geometry>
[...]
</feature:geometry>
</feature:features>
</gml:featureMember>
</wfs:FeatureCollection>

My code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(...);

XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("wfs", "http://www.opengis.net/wfs/");
nsm.AddNamespace("gml", "http://www.opengis.net/gml/");
nsm.AddNamespace("feature", "http://mapserver.gis.umn/edu/mapserver/");

XmlNodeList featureList = xmlDoc.SelectNodes("//feature:features", nsm);

The problem? featureList is empty - has zero nodes.

The namespaces you've given don't match up - the ones in the code all
have slashes at the end. The ones in the XML file don't.

Likewise you've got umn.edu in the XML, but umn/edu in the code.

Fix both of these problems and it'll work.
 
J

Jeff Dege

The namespaces you've given don't match up - the ones in the code all
have slashes at the end. The ones in the XML file don't.

Likewise you've got umn.edu in the XML, but umn/edu in the code.

Fix both of these problems and it'll work.

That was it, thanks.

--
Many companies that have made themselves dependent on [the equipment of a
certain major manufacturer] (and in doing so have sold their soul to the
devil) will collapse under the sheer weight of the unmastered complexity
of
their data processing systems.
-- Edsger W. Dijkstra, SIGPLAN Notices, Volume 17, Number
5
 

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

Similar Threads


Top