c# - Get value from node -
i have question) how can value specific node(info/weather/day/day_part type="День" /tempreture )
<?xml version="1.0" encoding="utf-8"?> <info lang="ru" xmlns:x="http://www.yandex.ru/xscript"> +<region id="213" lon="37.617671" lat="55.755768" zoom="10"/> <traffic lon="37.617671" lat="55.755768" zoom="10" region="213"></traffic> <weather region="213" climate="1"> <source xmlns:xi="http://www.w3.org/2001/xinclude">mb3d</source> <day xmlns:xi="http://www.w3.org/2001/xinclude"> <title>Москва</title> <country>Россия</country> <time_zone>europe/moscow</time_zone> <summer-time>0</summer-time> <sun_rise>06:51</sun_rise> <sunset>20:14</sunset> <daytime>d</daytime>+<date date="2013-04-05t00:00:00z"/> <day_part type="день" typeid="2"> <weather_type>облачно</weather_type> <weather_code>overcast</weather_code> <image>http://weather.yandex.ru/i/5.png</image> <image-v2 size="22x22">http://yandex.st/weather/v-1/i/icons/22x22/ovc_+6.png</image-v2> <image-v3 size="48x48">http://yandex.st/weather/v-1/i/icons/48x48/ovc.png</image-v3> <image_number>5</image_number> <wind_speed>5.0</wind_speed> <wind_direction id="se">юв</wind_direction> <dampness>70</dampness> <pressure>743</pressure> <temperature color="f2f0e6" class_name="t6">+5</temperature> <time_zone>europe/moscow</time_zone> <observation_time>17:30</observation_time> <observation>2013-04-05t17:30:00</observation> </day_part> <day_part type="вечер" typeid="3"></day_part> <day_part type="ночь" typeid="4"></day_part> <day_part type="утро" typeid="1"></day_part> <day_part type="день" typeid="2"></day_part> <night_short></night_short> <tomorrow></tomorrow> </day> <url xmlns:xi="http://www.w3.org/2001/xinclude">http://pogoda.yandex.ru/moscow/</url> </weather> </info>
i have code, how can specify need node type="День"
xelement elem = xdocument.element("info"); if (elem != null) foreach (var el in elem.elements("weather").elements("day").elements("day_part")) { var level = el.element("temperature").value; listbox1.items.add(level); };
this block of code returns 2 values(+5, +6) need one(+5) , throw exception.
first of - input xml not valid , can't loaded xdocument
. fixed be:
<?xml version="1.0" encoding="utf-8"?> <info lang="ru" xmlns:x="http://www.yandex.ru/xscript">+<region id="213" lon="37.617671" lat="55.755768" zoom="10" /> <traffic lon="37.617671" lat="55.755768" zoom="10" region="213"></traffic> <weather region="213" climate="1"> <source xmlns:xi="http://www.w3.org/2001/xinclude">mb3d</source> <day xmlns:xi="http://www.w3.org/2001/xinclude"> <title>Москва</title> <country>Россия</country> <time_zone>europe/moscow</time_zone> <summer-time>0</summer-time> <sun_rise>06:51</sun_rise> <sunset>20:14</sunset> <daytime>d</daytime> <date date="2013-04-05t00:00:00z"></date> <day_part type="день" typeid="2"> <weather_type>облачно</weather_type> <weather_code>overcast</weather_code> <image>http://weather.yandex.ru/i/5.png</image> <image-v2 size="22x22">http://yandex.st/weather/v-1/i/icons/22x22/ovc_+6.png</image-v2> <image-v3 size="48x48">http://yandex.st/weather/v-1/i/icons/48x48/ovc.png</image-v3> <image_number>5</image_number> <wind_speed>5.0</wind_speed> <wind_direction id="se">юв</wind_direction> <dampness>70</dampness> <pressure>743</pressure> <temperature color="f2f0e6" class_name="t6">+5</temperature> <time_zone>europe/moscow</time_zone> <observation_time>17:30</observation_time> <observation>2013-04-05t17:30:00</observation> </day_part> <day_part type="вечер" typeid="3"></day_part> <day_part type="ночь" typeid="4"></day_part> <day_part type="утро" typeid="1"></day_part> <day_part type="день" typeid="2"></day_part> <night_short></night_short> <tomorrow></tomorrow> </day> <url xmlns:xi="http://www.w3.org/2001/xinclude">http://pogoda.yandex.ru/moscow/</url> </weather> </info>
with kind of input can desired nodes values using xpathselectelements
extension method:
var results = xdocument.xpathselectelements("info/weather/day/day_part[@type='день']/temperature") .select(e => (string)e) .tolist();
for xml document shown above results
contains 1 string
value: +5
.
however, suggest searching day_part
element using typeid
attribute value instead of type
:
var results = xdocument.xpathselectelements("info/weather/day/day_part[@typeid=2]/temperature") .select(e => (string)e) .tolist();
the same results, less chance fail because of encoding.
you can fill listbox
:
foreach (var value in results)) { listbox1.items.add(value); };
Comments
Post a Comment