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] "Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:%23JYw9dAhDHA.616@TK2MSFTNGP11.phx.gbl...
> Steve Robinson wrote:
>
> > Just out of interest - is there an easier/quicker way to retrieve values
via
> > Xpath when namespaces are used?
>
> 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
>