0

I have a specific problem:

looking at

<observation realtime_start="2013-02-23" realtime_end="2013-02-23" date="1975-01-01" value="4917.2"/> <observation realtime_start="2013-02-23" realtime_end="2013-02-23" date="1976-01-01" value="5186.8"/> 

So i'm trying to get the value attribute of a specific date but

string xmlNode = root.SelectSingleNode("/observations/observation/@value").Value; 

using that gets me the first value (4917.2). How would I go about specifying that I want value from "1976-01-01" and 5186?

Thank you.

1
  • Do you really want to return 5186 or do you want the actual value (5186.8)? Commented Feb 23, 2013 at 18:31

1 Answer 1

1

This will do it:

string xmlNode = root.SelectSingleNode("/observations/observation[@date='1976-01-01']/@value").Value; 

What this says is to select the @value of the observation node whose date is 1976-01-01. And to get just the integer of @value, you will probably need to use whatever language you're doing this in (I'd bet it has a round down function). As @JLRishe observed and explained, your root appears to be a .NET XmlNode or XPathNavigator, which can only select the node, so the below won't work and will throw an exception. I've left this in to show that there are xpath functions for rounding.

string xmlNode = root.SelectSingleNode("floor(/observations/observation[@date='1976-01-01']/@value)").Value; 

If using the xpath function route (which it appears you can't use), and if you want to always round to the nearest integer, change floor to round. And if you want to always round up, change floor to ceiling.

Sign up to request clarification or add additional context in comments.

5 Comments

If the OP really wanted to return 5186 from 5186.8, round wouldn't work. floor() would be a better option.
Thanks, Daniel. I edited my answer just as you were posting your comment. :)
Assuming that root is a .NET XmlNode or XPathNavigator (which it appears to be), your second code snippet will produce an exception. SelectSingleNode() can only select nodes, as the name suggests.
Thanks, JLRishe. I've updated my answer to include your observation.
Yes it is .NET, sorry for not posting more info, but it worked! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.