Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 9th, 2006, 09:25 PM
Jiho Han
Guest
 
Posts: n/a
Default XPathNavigator.Select with a document that has default namespace only

I have an xml document as follows:

<?xml version="1.0" encoding="utf-8" ?>
<entityConfiguration xmlns="http://schemas.infinityinfo.com/entityconfiguration"
version="1.0">
<entity name="account">
<attribute name="accountid" />
</entity>
</entityConfiguration>

I am trying to find all 'entity' elements to iterate and the following are
the snippets:

// in static constructor
XmlDocument doc = new XmlDocument();
_configExpr = doc.CreateNavigator().Compile("/entityConfiguration/entity");
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("", SchemaNamespace);
_configExpr.SetContext(ns);

// then in a method (xvr is a XmlValidatingReader for the above xml)
XPathDocument xpath = new XPathDocument(xvr);
XPathNavigator nav = xpath.CreateNavigator();
XPathNodeIterator itr = nav.Select(_configExpr); // this returns zero nodes.

Basically, the node set returned by the expression is 0. However, if I do:

XPathNodeIterator itr = nav.SelectDescendants("entity", SchemaNamespace,
false);

Then, I will get the correct list of entity element nodes. I could also
move to entityConfiguration node and use SelectChildren("entity", SchemaNamespace)
with success.
Since Select is the only one that takes a compiled expression, I would like
to know why it's not working properly.

Thanks in advance.

Jiho Han
Senior Software Engineer
Infinity Info Systems
The Sales Technology Experts
Tel: 212.563.4400 x216
Fax: 212.760.0540
jhan@infinityinfo.com
www.infinityinfo.com


  #2  
Old August 10th, 2006, 02:25 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: XPathNavigator.Select with a document that has default namespaceonly



Jiho Han wrote:
Quote:
I have an xml document as follows:
>
<?xml version="1.0" encoding="utf-8" ?>
<entityConfiguration
xmlns="http://schemas.infinityinfo.com/entityconfiguration" version="1.0">
<entity name="account">
<attribute name="accountid" />
</entity>
</entityConfiguration>
>
I am trying to find all 'entity' elements to iterate and the following
are the snippets:
>
// in static constructor
XmlDocument doc = new XmlDocument();
_configExpr = doc.CreateNavigator().Compile("/entityConfiguration/entity");
_configExpr =
doc.CreateNavigator().Compile("/pf:entityConfiguration/pf:entity");
Quote:
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("", SchemaNamespace);
ns.AddNamespace("pf", SchemaNamespace);

With XPath 1.0 you always need a prefix bound to a namespace URI to
select elements in that namespace, even if elements in the XML document
are in a default namespace and have no prefix. You can choose the prefix
as you like when you use the XPath API, and no change to the input XML
is needed

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old August 11th, 2006, 05:05 PM
Jiho Han
Guest
 
Posts: n/a
Default Re: XPathNavigator.Select with a document that has default namespace only

Hello Martin,

That worked perfectly. Thank you.

Jiho
Quote:
Jiho Han wrote:
>
Quote:
>I have an xml document as follows:
>>
><?xml version="1.0" encoding="utf-8" ?>
><entityConfiguration
>xmlns="http://schemas.infinityinfo.com/entityconfiguration"
>version="1.0">
><entity name="account">
><attribute name="accountid" />
></entity>
></entityConfiguration>
>I am trying to find all 'entity' elements to iterate and the
>following are the snippets:
>>
>// in static constructor
>XmlDocument doc = new XmlDocument();
>_configExpr =
>doc.CreateNavigator().Compile("/entityConfiguration/entity");
_configExpr =
doc.CreateNavigator().Compile("/pf:entityConfiguration/pf:entity");
>
Quote:
>XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
>ns.AddNamespace("", SchemaNamespace);
>>
ns.AddNamespace("pf", SchemaNamespace);
>
With XPath 1.0 you always need a prefix bound to a namespace URI to
select elements in that namespace, even if elements in the XML
document are in a default namespace and have no prefix. You can choose
the prefix as you like when you use the XPath API, and no change to
the input XML is needed
>

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles