Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XPath problems using LINX to XML / XPathSelectElements

Posted on 2009-07-01
5
Medium Priority
?
3,957 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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

660 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