Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
XML:
XML
<response xmlns="abc">
	<errors>
		<error>Authentication failed</error>
	</errors>
</response>


Code:
C#
XNamespace ns = "abc";
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace(string.Empty, "abc");
string err = XDOC.Element(ns + "response").XPathSelectElement("errors/error", namespaceManager).Value;

but it gives me error "object reference not set to instance of an object"

please help.

it working fine if i used,
string err = XDOC.Element(ns + "response").Element(ns + "errors").Element(ns+"error").Value;

but i want to use XPathSelectElement.

thanks in advance..
Posted
Updated 20-Feb-11 23:08pm
v4

1 solution

XDOC.Element(ns + "response").XPathSelectElement("errors/error", namespaceManager).Value;

XDOC.Element(ns + "response") is null.
I dont seem to understand why ns + "response" is required.

Try XDOC.Element("response"),
 
Share this answer
 
v2
Comments
Punit Belani 21-Feb-11 5:00am    
because xml document have root element "response" with namespace "abc".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900