Solved

XPath problems using LINX to XML / XPathSelectElements

Posted on 2009-07-01
5
3,812 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now