Solved

XPath problems using LINX to XML / XPathSelectElements

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i use linq for parsing string datetime to real datetime? 16 44
XML SQL 8 25
Hey!! 5 33
MediaHelp 4 23
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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