Null returned when selecting a node in XML document?

The problem is with this statement: doc. SelectSingleNode("s:Requestor", nsmgr) what you need to do is doc. SelectSingleNode("//s:Requestor",nsmgr) "s:Requestor" means give me the node underneath the current node name s:Requestor "//s:Requestor" means give me all nodes in the document named s:Requestor if you want to ignore the namespace you could do doc.

SelectSingleNode("//*local-name()='Requestor'").

1 Fabulous Greg! The option to ignore the NS you suggested worked and is what I prefer, since I can't control what namespace the 3rd party webservice assigns. I tried the "//s:Requestor" but it didn't work.

– Don Jun 9 '09 at 21:54.

I'm not sure whether I have understood your task correctly. But if you have to just remove the xsi:nil="true" part, why don't you load it as a string and invoke a string. Replace("xsi:nil=\"true\"", "") Of course that's not the cleanest solution, but I'm not yet comfortable with the XML handling API of C#, so I'd have to consult the MSDN to get accustomed.

Maybe this solves already your issue.

Thanks, Juri. The problem I'm having is that the variable "node" is not getting set, and when the RemoveAll() method is called, it throws the exception. Can you see what is wrong with my XPath syntax so that the "node" variable gets set to the correct element?

– Don Jun 9 '09 at 21:38.

There are two errors in your code. One is the XPath used for selecting the node (as Greg pointed out). The second one is the name space.

I may be wrong, but as I interpret the XML document the TaskData element has the following namespace declaration: xmlns="schema.sample.com/application/1/520800B" ...which sets up the namespace for elements without prefixes. But then the RequestInfo tag has this namespace declaration: xmlns="schema.sample.com/application/1/Types" So within the RequestInfo tag, there is another namespace for tags without prefixes. In your code you use the first of these two namespaces to match a tag that resides in the second, which will not work out.

There are two ways to solve it. One is to simply change the namespace in your code: XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc. NameTable); nsmgr.

AddNamespace("s", "schema.sample.com/application/1/Types"); XmlNode node = doc. SelectSingleNode("//s:Requestor", nsmgr); The second one is to define both namespaces, and use an XPath expression that points out the full path to the tag: XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc. NameTable); nsmgr.

AddNamespace("r", "schema.sample.com/application/1/520800B"); nsmgr. AddNamespace("s", "schema.sample.com/application/1/Types"); XmlNode node = doc. SelectSingleNode(@"/r:TaskData/r:Global/s:RequestInfo/s:Requestor", nsmgr); Both these cases lead to the same result; node is not null.

Fredrik, I like having the ability to specify the full path for the element. Since this is my first post, I don't have enough reputation to vote on your answer yet, but it is very helpful, thank you! – Don Jun 9 '09 at 22:38.

You should be able to opt out of any namespace calls in your selectelement call, I prefer to loop through my document anyway.. but that's just me.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions