Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

c#, xml parsing

Posted on 2010-01-10
3
Medium Priority
?
336 Views
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";
        doc.Load(Server.UrlPathEncode(varURL));

        XmlNode root = doc.DocumentElement;
        ZEstimate.Text = root.SelectSingleNode("/response/results/result/zpid").ChildNodes[0].Value;
0
Comment
Question by:bob1barker
  • 2
3 Comments
 
LVL 64

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.

Fernando
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

0
 
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...
0
 
LVL 64

Accepted Solution

by:
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;

Fernando
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

564 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