XML Question

  • Thread starter Thread starter C# newbie
  • Start date Start date
C

C# newbie

hi,

when I open an xml file which starts like:

<?xml version="1.0"?>
<Survey >
some xml code
some xml code
some xml code
some xml code
some xml code
</Survey>

Xpath works fine.

But when I open same xml with a starting point as this: (xpath doen't work):

<?xml version="1.0"?>
<Survey SurveyInfo="empty" SurveyID="1" StyleUsage="URL"
xmlns="http://www.domain.com" SurveyCoder="somebody" DateTime="12/10/2003
4:00:00 PM">
some xml code
some xml code
some xml code
some xml code
some xml code
</Survey>
any clue?

thx
 
C# newbie said:
But when I open same xml with a starting point as this: (xpath doen't work):

<?xml version="1.0"?>
<Survey SurveyInfo="empty" SurveyID="1" StyleUsage="URL"
xmlns="http://www.domain.com" SurveyCoder="somebody"

It's the default namespace, http://www.domain.com.

What you must do is register a namespace URI with an XmlNamespace-
Manager and pass that to one of the SelectSingleNode( ) overloads that
accepts a namespace manager.

- - - DefaultNamespaceQuery.cs (excerpt)
// . . .
XmlNamespaceManager nsMan = new XmlNamespaceManager( new NameTable());

nsMan.AddNamespace( "", "http://www.domain.com");

XmlNode result = xmlDoc.SelectSingleNode( "/Survey", nsMan);
// . . .
- - -

A common misconception is that the prefix registered must match
the prefix as it appears in the XML instance document, but that is
not the case. Prefixes are arbitrary; it is the namespace URI that
matters. So as long as the nsURI is http://www.domain.com I could
also use the domain: prefix as follows,

- - - NamespacePrefixQuery.cs (excerpt)
// . . .
XmlNamespaceManager nsMan = new XmlNamespaceManager( new NameTable());

nsMan.AddNamespace( "domain", "http://www.domain.com");

XmlNode result = xmlDoc.SelectSingleNode( "/domain:Survey", nsMan);
// . . .
- - -


Derek Harmon
 
Back
Top