Alex Gordon
Alex Gordon

Reputation: 60751

xml error: Object reference not set to an instance of an object after SelectSingleNode

here's my code:

XmlDocument doc = new XmlDocument();
foreach (string c in colorList)
{
     doc.Load(@"http://whoisxmlapi.com/whoisserver/WhoisService?domainName=" + c + @"&username=user&password=pass");
     textBox1.Text += doc.SelectSingleNode("WhoisRecord/registrant/email").InnerText + ",";
}

for the second line of code (textbox1...) is generating this error what am i doing wrong?

Upvotes: 3

Views: 7884

Answers (5)

Jono
Jono

Reputation: 2054

Browsers do all sorts of other clever stuff like following redirects and dealing with sessions. How about stepping into the code and having a look at the XmlDocument's OuterXml property for the document that fails?

Upvotes: 0

Zach Johnson
Zach Johnson

Reputation: 24232

How about splitting up the line to see where the exception occurs?

// if node is null the problem is with SelectSingleNode 
XmlNode node = doc.SelectSingleNode("WhoisRecord/registrant/email");

// if text is null the problem is with the node 
string text = node.InnerText;

// if textBox1 is null the problem is with textBox1
textBox1.Text += text + ",";

Upvotes: 6

Alex
Alex

Reputation: 14618

it appears that doc.SelectSingleNode("WhoisRecord/registrant/email") is null. Can't get property of a null.

Upvotes: 0

ChaosPandion
ChaosPandion

Reputation: 78272

The only reason you would get a NullReferenceException is if the XPath query is returning null. Examine the XML before running the query to see what the issue is.

Upvotes: 0

Lee
Lee

Reputation: 144136

The documentation for SelectSingleNode states it returns null if no matching node is found. You'll have to fix the query or handle a failure to find a match.

Upvotes: 3

Related Questions