c# MLS Parsing, empty value error

Posted on 2010-01-11
Medium Priority
Last Modified: 2013-11-18
I am parsing an XML file on the fly to show a single value inside the XML file. The code I have works great until we find a record that has no value in the xml file.  I have tried a few things to make it only show up if there was a value but I haven't been able to figure it out.

any thoughts?

you can test it by changing the XML URL to a fake address and you will see the value I am using disappear.

XmlDocument doc = new XmlDocument();
        string varURL = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1cwo6lcszyj_aui2x&address=12598+Dupont+Drive&citystatezip=Rancho+Cucamonga+91739";

        XmlNode root = doc.DocumentElement;
        ZEstimate.Text = root.SelectSingleNode("//response/results/result/zpid").ChildNodes[0].Value;
Question by:bob1barker
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 63

Accepted Solution

Fernando Soto earned 2000 total points
ID: 26283849
Hi bob1barker;

You can use a try/catch statement and test root to see if it null before displaying info.

XmlDocument doc = new XmlDocument();
string varURL = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1cwo6lcszyj_aui2x&address=12598+Dupont+Drive&citystatezip=Rancho+Cucamonga+91739";

XmlNode root = null;
string message = String.Empty;
    root = doc.DocumentElement;
    message = "Error getting info.";

if( !(root == null) )
    ZEstimate.Text = root.SelectSingleNode("//response/results/result/zpid").ChildNodes[0].Value;
    ZEstimate.Text = message;

Open in new window


Author Closing Comment

ID: 31675496
Worked Perfect
LVL 63

Expert Comment

by:Fernando Soto
ID: 26294116
Not a problem, glad to help.  ;=)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question