How do I use XPath with a default namespace with no prefix?

Posted by Scott Stafford on Stack Overflow See other posts from Stack Overflow or by Scott Stafford
Published on 2010-03-26T16:15:24Z Indexed on 2010/03/26 19:23 UTC
Read the original article Hit count: 190

Filed under:
|
|
|

What is the XPath (in C# API to XDocument.XPathSelectElements(xpath, nsman) if it matters) to query all MyNodes from this document?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <MyNode xmlns="lcmp" attr="true">
    <subnode />
  </MyNode>
</configuration
  • I tried /configuration/MyNode which is wrong because it ignores the namespace.
  • I tried /configuration/lcmp:MyNode which is wrong because lcmp is the URI, not the prefix.
  • I tried /configuration/{lcmp}MyNode which failed because Additional information: '/configuration/{lcmp}MyNode' has an invalid token.

EDIT: I can't use mgr.AddNamespace("df", "lcmp"); as some of the answerers have suggested. That requires that the XML parsing program know all the namespaces I plan to use ahead of time. Since this is meant to be applicable to any source file, I don't know which namespaces to manually add prefixes for. It seems like {my uri} is the XPath syntax, but Microsoft didn't bother implementing that... true?

© Stack Overflow or respective owner

Related posts about c#

Related posts about xpath