c#, xml parsing

Posted on 2010-01-10
Medium Priority
Last Modified: 2012-05-08
I am trying to parse a dynamic xml file and display just a single piece of the results but it keeps erroring our.  any suggestions?

 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

Expert Comment

by:Fernando Soto
ID: 26278550
Hi bob1barker;

I do not know XML well enough to see why your code is not working but if you are using Visual Studio 2008 .Net Framework 3.5 then using Linq to XML works much better. See code snippet below.

using System.Xml;
using System.Xml.Linq;

// Load the XML document from the URL 
XDocument doc = XDocument.Load("http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1cwo6lcszyj_aui2x&address=12598+Dupont+Drive&citystatezip=Rancho+Cucamonga+91739");
// Get reference to the Root node
XElement root = doc.Root;
// Get the value of the XML node with the tag name zpid and display it in the text box
ZEstimate.Text = root.Descendants("zpid").FirstOrDefault().Value;

Open in new window

LVL 96

Expert Comment

by:Bob Learned
ID: 26278560
It might help to know the XML that you are working with, and the error that you are getting.  The usual culprit is that you don't have a valid XPath expression, and nothing is selected...
LVL 63

Accepted Solution

Fernando Soto earned 2000 total points
ID: 26278573
I found out how to make your code work:

Change this line in your code:
ZEstimate.Text = root.SelectSingleNode("/response/results/result/zpid").ChildNodes[0].Value;

To this:
ZEstimate.Text = root.SelectSingleNode("//response/results/result/zpid").ChildNodes[0].Value;


Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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