all groups > dotnet xml > september 2003 >
You're in the

dotnet xml

group:

Namespaces & .NET XPaths


Namespaces & .NET XPaths Steve Robinson
9/25/2003 10:22:42 PM
dotnet xml:
Just out of interest - is there an easier/quicker way to retrieve values via
Xpath when namespaces are used?

At the moment I seem to need a lot more lines of code to do something I
thought was fairly standard? Of course I can just wrap the calls up into a
helper function but was wondering if there was an easier way...

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("xs", http://mynamespace.com);

node = doc.SelectSingleNode("//xs:person",nsmgr);

... pluck from node...

OR

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("xs", http://mynamespace.com);

xpExp = nav.Compile("string(//xs:person/@name)");

xpExp.SetContext(xmlNSM);

string name = nav.Evaluate(xpExp).ToString(); (BTW: is ToString()
any different to casting (string) )



Re: Namespaces & .NET XPaths Oleg Tkachenko
9/26/2003 11:28:10 AM
[quoted text, click to view]

If you mean using NamespaceManager - no, you cannot avoid using it when
seleting namespaced nodes, unless your XPath expression is
namespace-prefix-neutral, like
//*[local-name='person' and namespace-uri()='http://mynamespace.com']
instead of
//xs:person

And in fact I don't see any problem with using NamespaceManager - anyway
must be some way to say what xs namespace prefix you are using points
to, becuase arbitrary XML doc may declare xs namespace prefix twice,
each time binding it to a different namespace URI's:
<xs:foo xmlns:xs="foo">
<xs:bar xmlns:xs="bar">blah</xs:bar>
</xs:foo>
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Re: Namespaces & .NET XPaths Steve Robinson
9/26/2003 6:56:19 PM
I guess I just hate having to compile an expression when I do an Evaluate
rather than Select.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("xs", http://mynamespace.com);
xpExp = nav.Compile("string(//xs:person/@name)");
xpExp.SetContext(xmlNSM);
string name = (string) nav.Evaluate(xpExp);

if I need to pull another value out I need to compile a new expression,
setcontext and Evaluate again...and so on...

xpExp = nav.Compile("string(//xs:person/@department)");
xpExp.SetContext(xmlNSM);
string department = (string) nav.Evaluate(xpExp);

I could always wrap it all up in helper functions or a class, but Ideally it
would have been good if I could at least reuse the same expression object
and just the path or SetContext() would remain valid -

nav.SetContext(namespace); // set namespace for xpath
val1 = nav.Evaluate();
val2 = nav.Evaluate();




[quoted text, click to view]

Re: Namespaces & .NET XPaths Oleg Tkachenko
9/29/2003 12:41:26 PM
[quoted text, click to view]

Well, I tend to agree with you, but... Let's wait PDC and see what they have
prepared in V2 API for us.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
AddThis Social Bookmark Button