Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3986
  • Last Modified:

XPath problems using LINX to XML / XPathSelectElements

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
sahl04
Asked:
sahl04
  • 3
1 Solution
 
abelCommented:
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
 
sahl04Author Commented:
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
 
abelCommented:
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
 
abelCommented:
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now