?
Solved

parsing XML file in c#

Posted on 2010-01-12
1
Medium Priority
?
250 Views
Last Modified: 2013-11-18
I know this is going to be easy but I am stumped

I have an xml file that I am parsing.  I have it all set up great and it works all the way until there is a record that is missing data

let me give an example

I am only concerned about the AMOUNT field

normally the XML file would look like this

- <zestimate>
  <amount currency="USD">151515</amount>
  <last-updated>01/11/2010</last-updated>
  <oneWeekChange deprecated="true" />
  <valueChange />
- <valuationRange>
  <low currency="USD" />
  <high currency="USD" />
  </valuationRange>
  <percentile>0</percentile>
  </zestimate>


the snag is when there is no value in amount, it looks like this

- <zestimate>
  <amount currency="USD" />
  <last-updated>01/11/2010</last-updated>
  <oneWeekChange deprecated="true" />
  <valueChange />
- <valuationRange>
  <low currency="USD" />
  <high currency="USD" />
  </valuationRange>
  <percentile>0</percentile>
  </zestimate>

and that causes an error in my file



here is my code
zestval = root.SelectSingleNode("//response/results/result/zestimate/amount").ChildNodes[0].Value;

I have tried

if (root.SelectSingleNode("//response/results/result/zestimate/amount").ChildNodes[0].Value != null)
      zestval = root.SelectSingleNode("//response/results/result/zestimate/amount").ChildNodes[0].Value;
else
      zestval = "0";

and when I do a Response.Write(zestval);
nothing comes up



who would I parse it so if there is NO value in amount it just puts a zero?  


0
Comment
Question by:bob1barker
[X]
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
1 Comment
 

Accepted Solution

by:
bob1barker earned 0 total points
ID: 26298200
I solved it with a

if (root.SelectSingleNode("//response/results/result/zestimate/amount").InnerXml == "")
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

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