tags:

views:

104

answers:

5

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?

+3  A: 

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.

Lee
it works fine if i just paste the URL into the browser
I__
I bet you didn't paste the SelectSingleNode call into your browser, since that's the thing that's failing. The capitalization in your XPath looks suspicious, you sure it's correct?
Matti Virkkunen
Besides... what did you paste for the "c" variable used?The XPath is missing an element (registryData) between 'WhoisRecord' and 'registrant'. I also noticed when you select a domain that is 'available' the registrant element will be empty and therefore cause the mentioned 'Null' reference exception.
Marvin Smit
A: 

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.

ChaosPandion
it works fine if i just paste the URL into the browser
I__
@every_answer_gets_a_point - Check it in code.
ChaosPandion
A: 

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

Alexander
it works fine if i just paste the URL into the browser
I__
you can debug, decompose the code, make variables for each intermediary value, check if their value corresponds to what you're expecting.
Alexander
+2  A: 

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 + ",";
Zach Johnson
A: 

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?

Jono