XmlDocument parsing using SelectSingleNode()

D

David Veeneman

I've used the XmlDocument class for some time to parse XML documents. After
being away from it for a while, I have come across a type of XML file it
doesn't seem to work with, although the XML seems perfectly valid. I'm
hoping someone in this forum can point out the flaw in my logic that I know
must be causing the problem.

Here's the code that I'm using to parse the XML:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"D:\DCV Documents\Visual Studio
2005\Projects\GeoTagger\TestGpx\HHV House.gpx");
XmlNode root = xmlDocument.DocumentElement;
XmlNode wptNode = root.SelectSingleNode("wpt");

The root node parses correctly, but wptNode is null, even after the call to
SelectSingleNode(). What am I doing wrong?

Here is the XML markup that I am trying to parse:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.12.4"
version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd">

<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>2007-09-29T02:21:05Z</time>
<bounds maxlat="40.2492361" maxlon="-105.8391159"
minlat="40.2492361" minlon="-105.8391159" />
</metadata>

<wpt lat="40.2492361" lon="-105.8391159">
<ele>2603.1157227</ele>
<name>TestLocation</name>
<cmt>22-SEP-07 12:33:08AM</cmt>
<desc>22-SEP-07 12:33:08AM</desc>
<sym>Trail Head</sym>
</wpt>

</gpx>

Thanks in advance for your help!
 
R

raylopez99

I've used the XmlDocument class for some time to parse XML documents. After
being away from it for a while, I have come across a type of XML file it
doesn't seem to work with, although the XML seems perfectly valid. I'm
hoping someone in this forum can point out the flaw in my logic that I know
must be causing the problem.

Here's the code that I'm using to parse the XML:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"D:\DCV Documents\Visual Studio
2005\Projects\GeoTagger\TestGpx\HHV House.gpx");
XmlNode root = xmlDocument.DocumentElement;
XmlNode wptNode = root.SelectSingleNode("wpt");

The root node parses correctly, but wptNode is null, even after the call to
SelectSingleNode(). What am I doing wrong?

Keeping in mind I've only got a few weeks of C# experience under my
belt, one thing you MAY be doing wrong is that you're not using 'new'
correctly.

You have to use 'new' everytime, everywhere in C#.

Example:

MyClass[ ] myClassArray = new MyClass[1000]; //array of 1000 "MyClass"
classes

for (int i=0; i <1000; i++) {
myClassArray = new MyClass(); //REQUIRED!!!
}

Do you see the subtlety here? "new" is used "TWICE", once for the
array, once for the member (a class) inside the array.

Stuff like that.

Hope this helps--reread your code with this in mind.

Ray Lopez
[C# MVP, of sorts]
 
M

Marc Gravell

Namespaces; the doc declares a default namespace, which complicates
querying somewhat, in that you need to be explicit. There may be an
easier way, but something like the following works (by declaring a
prefix for the default namespace, and then using it in the query):

XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", root.NamespaceURI); // x is our temp
alias
XmlNode wptNode = root.SelectSingleNode("x:wpt",nsmgr);
 
D

David Veeneman

Thanks, but let me clarify for the benefit of others who may read this
thread later. The use of 'new' isn't the issue here. The code works fine on
other XML documents. The issue appears to be the declaration of a namespace
by the XML document. See other replies in this thread.

Also, and please don't take offense, the 'MVP' designation is awarded by
Microsoft to very experienced programmers. I don't think they would confer
that status on someone with just a few weeks experience in C#. I simply
don't want a later reader to be confused.

Be assured, I do appreciate the effort to be helpful!
 
R

raylopez99

Also, and please don't take offense, the 'MVP' designation is awarded by
Microsoft to very experienced programmers. I don't think they would confer
that status on someone with just a few weeks experience in C#. I simply
don't want a later reader to be confused.

I didn't know that--lern something "new" everyday!

Yes, re this thread, the scope resolution operator is always a
problem...and I notice C# is very strict about naming conventions,
often balking even though the names are in different scopes it seems
to me... perhaps the 'namespace' issue confuses things for the
compiler.

RL
 
M

Marc Gravell

Yes, re this thread, the scope resolution operator is always a
problem...and I notice C# is very strict about naming conventions,
often balking even though the names are in different scopes it seems
to me... perhaps the 'namespace' issue confuses things for the
compiler.

Sorry, but in this context "namespace" is referring to xml namespaces,
not to .NET namespaces (nor to C#); this issue is nothing to do with
the compiler, but is a consequence of the full specification for xml /
xquery / xpath, as used in the SelectNodes() / SelectSingleNode()
method of an XmlNode.

Marc
 

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