Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi There,

I want to read Country node from this online xml :

http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=49.14.147.196&LicenseKey=0[^]

See What I Tried -
C#
XmlDocument document = new XmlDocument();
document.Load("http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=49.14.147.196&LicenseKey=0"); 
XmlNode node = document.SelectSingleNode("/IPInformation/Country");
string name = node.InnerText;


But not succeeded, Getting Error in string name - Object reference not set to an instance of an object, XmlNode node is also null.
Posted
Updated 26-Mar-15 7:43am
v3
Comments
ZurdoDev 26-Mar-15 13:40pm    
If you get an error tell us where.

I'm guessing it is on trying to select the IPInformation node. That means it isn't there.
Pr!y@ 26-Mar-15 13:42pm    
variable node is null and error is in string name
ZurdoDev 26-Mar-15 13:44pm    
node is null because it cannot find /IPInformation/Country
Pr!y@ 26-Mar-15 13:45pm    
Please see xml format clicking link
ZurdoDev 26-Mar-15 13:46pm    
I have no idea what that means.

The reason node is null is because there is nothing at the path you are trying to select. Your path is wrong.

1 solution

The returned XML document has a default namespace. Unfortunately, the XmlDocument doesn't make it easy to select nodes with a default namespace. You need to create an XmlNamespaceManager, add a namespace prefix for the default namespace, and change your XPath expression to use the new namespace prefix:
C#
XmlDocument document = new XmlDocument();
document.Load("http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=49.14.147.196&LicenseKey=0"); 

XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("x", document.DocumentElement.NamespaceURI);

XmlNode node = document.SelectSingleNode("/x:IPInformation/x:Country", namespaceManager);
string name = node.InnerText;
 
Share this answer
 
Comments
Pr!y@ 26-Mar-15 13:58pm    
Thanks Richard Sir.

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