Solved

XPath problems using LINX to XML / XPathSelectElements

Posted on 2009-07-01
5
3,929 Views
Last Modified: 2013-11-11
Dear all,

perhaps just a small problem but i can't resolve it.

I am trying to select XElement objects using XPath expressions with the XPathSelectElements method.
It seems that there is a problem with the used namespace.
See attached code to see the principle for what i am doing.
The childList-variable is empty after executing the XPathSelectElements-method.

Best regards
sahl04
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(String.Empty, "www.adventure-works.com"); //default namespace
 
XElement root = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>");
 
List<XElement> childList = root.XPathSelectElements("//Child", nsmgr).ToList();

Open in new window

0
Comment
Question by:sahl04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24754025
The namespace you use for the default namespace is not correct. In the same code, you use both "www.example.com" and "http://www.example.com". Namespaces have little to do with real domains and should be seen as literal strings (though they must obey the URI standard, which means it must start with "http:" or "urn:", they should not point to real resources online). If the namespace has one letter different, it will not match. In your case, add "http:".

-- Abel --
0
 

Author Comment

by:sahl04
ID: 24754238
Dear Abel,

now i tried different versions of the namespace notations but no success.
Current version of code is attached.

Best regards
sahl04

NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(String.Empty, "urn:example"); //default namespace
 
XElement root = XElement.Parse(
@"<Root xmlns='urn:example'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>");
 
List<XElement> list = root.XPathSelectElements("//Child", nsmgr).ToList();

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 250 total points
ID: 24755294
I had to experiment quite a bit to come to the conclusion that XPathSelectElement(s) does not work well with default namespace when selected as such in a namespace manager. This is a bug with the method (but I couldn't find a reference backing up my story). It is easy to test, however.

It took so long because I was for a long time fooled to believe that I could select Root, but that's not possible, it is the current element, but using "/ex:Root" or "//ex:Root" does not yield anything. This struck (and strikes) me as odd, but that's the way XElement works.

Note in the example below that you do not need to change your XML to reflect the same prefix. It is the namespace that must match, not the prefix.

-- Abel --

// does not work
nsmgr.AddNamespace(String.Empty, "urn:example");
List<XElement> list = root.XPathSelectElements("//Child", nsmgr).ToList();
 
// works:
nsmgr.AddNamespace("ex", "urn:example");
List<XElement> list = root.XPathSelectElements("//ex:Child", nsmgr).ToList();

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24755521
can you explain me why you graded B? Was there missing information? Was it not complete? I can't help that Microsoft has bugs, don't spawn your disappointment on the author of the answer.

Information on reopening questions: http://www.experts-exchange.com/help.jsp?hi=404


0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question