Reputation: 403
In an XML document, I have 3 different elements, all named "time". How do I select the third element in this XML document? (that is named time) Doing this only selects the first one:
xDoc.Root.Element("forecast").Element("time").Element("temperature").Attribute("day").Value
By the way, the XML elements named "Time" have different dates attached to them via attribute "Day", if that is any use.
<time day="2013-12-23">
</time>
<time day="2013-12-24">
</time>
Whole XML per request:
<weatherdata>
<location>
<name>London</name>
<type/>
<country>GB</country>
<timezone/>
<location altitude="0" latitude="51.50853" longitude="-0.12574" geobase="geonames" geobaseid="0"/>
</location>
<credit/>
<meta>
<lastupdate/>
<calctime>0.0036</calctime>
<nextupdate/>
</meta>
<sun rise="2013-12-24T08:05:11" set="2013-12-24T15:55:37"/>
<forecast>
<time day="2013-12-23">
<symbol number="501" name="moderate rain" var="10d"/>
<precipitation value="4" type="rain"/>
<windDirection deg="205" code="SSW" name="South-southwest"/>
<windSpeed mps="13.66" name="Strong breeze"/>
<temperature day="11.22" min="11.22" max="11.43" night="11.43" eve="11.22" morn="11.22"/>
<pressure unit="hPa" value="989.46"/>
<humidity value="94" unit="%"/>
<clouds value="overcast clouds" all="92" unit="%"/>
</time>
<time day="2013-12-24">
<symbol number="501" name="moderate rain" var="10d"/>
<precipitation value="8.5" type="rain"/>
<windDirection deg="216" code="SW" name="Southwest"/>
<windSpeed mps="9.26" name="Fresh Breeze"/>
<temperature day="9.79" min="6.09" max="10.36" night="6.43" eve="6.09" morn="10.36"/>
<pressure unit="hPa" value="984.45"/>
<humidity value="97" unit="%"/>
<clouds value="overcast clouds" all="92" unit="%"/>
</time>
<time day="2013-12-25">
<symbol number="500" name="light rain" var="10d"/>
<precipitation value="1" type="rain"/>
<windDirection deg="162" code="SSE" name="South-southeast"/>
<windSpeed mps="4.31" name="Gentle Breeze"/>
<temperature day="7.23" min="4.15" max="7.65" night="5.4" eve="6.98" morn="4.15"/>
<pressure unit="hPa" value="986.23"/>
<humidity value="100" unit="%"/>
<clouds value="scattered clouds" all="32" unit="%"/>
</time>
<time day="2013-12-26">
<symbol number="802" name="scattered clouds" var="03d"/>
<precipitation/>
<windDirection deg="253" code="WSW" name="West-southwest"/>
<windSpeed mps="8.77" name="Fresh Breeze"/>
<temperature day="6.41" min="3.7" max="6.84" night="4.68" eve="5.06" morn="4.86"/>
<pressure unit="hPa" value="993.13"/>
<humidity value="92" unit="%"/>
<clouds value="scattered clouds" all="48" unit="%"/>
</time>
<time day="2013-12-27">
<symbol number="501" name="moderate rain" var="10d"/>
<precipitation value="6" type="rain"/>
<windDirection deg="208" code="SSW" name="South-southwest"/>
<windSpeed mps="13.51" name="Strong breeze"/>
<temperature day="10.34" min="7.8" max="11.04" night="7.8" eve="8.81" morn="10.59"/>
<pressure unit="hPa" value="977.27"/>
<humidity value="93" unit="%"/>
<clouds value="scattered clouds" all="32" unit="%"/>
</time>
<time day="2013-12-28">
<symbol number="800" name="sky is clear" var="01d"/>
<precipitation/>
<windDirection deg="261" code="W" name="West"/>
<windSpeed mps="3.47" name="Gentle Breeze"/>
<temperature day="7.92" min="2.36" max="7.92" night="2.36" eve="3.82" morn="7.02"/>
<pressure unit="hPa" value="1000.76"/>
<humidity value="82" unit="%"/>
<clouds value="sky is clear" all="0" unit="%"/>
</time>
<time day="2013-12-29">
<symbol number="500" name="light rain" var="10d"/>
<precipitation value="0.46" type="rain"/>
<windDirection deg="263" code="W" name="West"/>
<windSpeed mps="6.9" name="Moderate breeze"/>
<temperature day="9.13" min="8" max="10.41" night="10.41" eve="8.79" morn="8"/>
<pressure unit="hPa" value="1013.2"/>
<humidity value="0" unit="%"/>
<clouds value="few clouds" all="17" unit="%"/>
</time>
</forecast>
</weatherdata>
Upvotes: 0
Views: 2693
Reputation: 66459
Get the value from the third "time" element in your XML:
xDoc.Root.Element("forecast")
.Elements("time")
.Skip(2).First()
.Element("temperature")
.Attribute("day")
.Value;
Or if you prefer to search by the date (more reliable):
xDoc.Root.Element("forecast")
.Elements("time")
.Single(x => x.Attribute("day").Value == "2013-12-25")
.Element("temperature")
.Attribute("day")
.Value;
If there's any chance you could be searching for a date that exists multiple times in the file, you'll need First
as Single
blows up if there's more than a single record.
If there's any chance of searching for a date that doesn't exist in your XML at all, look into SingleOrDefault
or FirstOrDefault
, as those return null
if the record doesn't exist instead of throwing an exception. If that's the case, you'll need to split the above query into two parts, testing for null before trying to get the day's temperature.
Upvotes: 1
Reputation: 20014
Using your XML Sample, this is what you can do to obtain only the third element.
IEnumerable<XElement> times = (
from item in xDoc.Root.Element("forecast").Elements("time")
select item).Skip(2).Take(1);
foreach (XElement el in times)
Console.WriteLine(el);
Hope this helps
Upvotes: 1