Viewed 63k times. How do I get SelectSingleNode to return the relevant element? Improve this question. Kate Orlova 2, 5 5 gold badges 10 10 silver badges 28 28 bronze badges. Shlomi Komemi Shlomi Komemi 5, 3 3 gold badges 26 26 silver badges 40 40 bronze badges.
Add a comment. Active Oldest Votes. NameTable ; ns. Improve this answer. Is this applied to SelectNodes? SelectSingleNode "xpath" is roughly equivalent to SelectNodes "xpath" [0]. Alex, that's arbitrary, I could have used anything. The important thing is to use the same name in the call to AddNamespace and in the namespace prefix. NameTable ; foreach XmlAttribute curAttribute in xml. AddNamespace curAttribute. LocalName, curAttribute.
How to omit all that namespace nonsense, tha complicates the simple, when we are sure the source xml is consistent and homogeneous? Show 2 more comments. I strongly suspect the problem is to do with namespaces. Try getting rid of the namespace and you'll be fine - but obviously that won't help in your real case, where I'd assume the document is fixed. I can't remember offhand how to specify a namespace in an XPath expression, but I'm sure that's the problem.
It's not terribly pleasant though - you need to create an XmlNamespaceManager for it. Here's some sample code that works with your sample document:.
I fully admit that this is not a best-practice answer, but but it's an easy fix and sometimes that's all we need. In fact, whether here or in web services, getting null back from an XPath operation or anything that depends on XPath usually indicates a problem with XML namespaces.
I had the same issue and it was a headache. Since I didn't care much about the namespace or the xml schema, I just deleted this data from my xml and it solved all my issues. Todd Richardson Todd Richardson 1, 1 1 gold badge 11 11 silver badges 22 22 bronze badges.
Add a comment. Active Oldest Votes. NameTable ; manager. Improve this answer. Mikko Rantanen Mikko Rantanen 7, 1 1 gold badge 28 28 silver badges 43 43 bronze badges.
Answering my own question. XmlNameTable is used for optimizing string comparisons and I guess the reason you can give the XmlNamespaceManager an existing name table allows fast comparisons with the XmlDocument elements as these then share the NameTable.
Your update seems to make sense. I was trying to put up an abbreviated form of the xml, and forgot to post the namespace. Thank you for taking the extra time to try and figure this one out. Once I removed the namespace from the original xml, everything works out like it should. Definitely worth more study of the System. Xml namespace. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password.
0コメント