Reputation: 55
I try to evaluate simple xpath
"//pr:Name"
for this xml
<?xml version="1.0" encoding="utf-8"?>
<pr:Products xmlns:pr="http://www.example.com/">
<pr:Name>Coffee</pr:Name>
</pr:Products>
but I get xml file in run-time i.e. I can't fill XmlNamespaceManager using method AddNamespace() (this why evaluating failed). I tried to do this trick
XDocument xdoc;
XmlReader reader;
using (var stream = new FileStream("test.xml", FileMode.Open))
{
reader = new XmlTextReader(stream);
xdoc = XDocument.Load(reader);
}
var nsManager = new XmlNamespaceManager(reader.NameTable);
var xpath = "//pr:Name";
var xvalue = xdoc.XPathEvaluate(xpath, nsManager);
but it does not help me. Do you have any idea how to resolve namespaces for XPath or evaluate XPath in other way? Thank you!
Upvotes: 1
Views: 704
Reputation: 56162
If you don't know namespace at compile time, you might probably use this XPath:
//*[local-name() = 'Name']
This XPath selects all Name
elements.
Upvotes: 1