C# 选择多个不返回结果
我在使用.SelectMany挖掘xml文件时遇到问题 xml结构是C# 选择多个不返回结果,c#,xml,linq,C#,Xml,Linq,我在使用.SelectMany挖掘xml文件时遇到问题 xml结构是 <data> <request> <type>LatLon</type> <query>Lat 48.85 and Lon 2.35</query> </request> <current_condition> <observation_time>12:38 PM</observ
<data>
<request>
<type>LatLon</type>
<query>Lat 48.85 and Lon 2.35</query>
</request>
<current_condition>
<observation_time>12:38 PM</observation_time>
<isdaytime>yes</isdaytime>
<temp_C>16</temp_C>
<temp_F>61</temp_F>
<weatherCode>116</weatherCode>
<weatherIconUrl>
<![CDATA[
http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png
]]>
</weatherIconUrl>
<weatherDesc>
<![CDATA[ Partly Cloudy ]]>
</weatherDesc>
<windspeedMiles>7</windspeedMiles>
<windspeedKmph>11</windspeedKmph>
<winddirDegree>20</winddirDegree>
<winddir16Point>NNE</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>51</humidity>
<visibility>10</visibility>
<pressure>1018</pressure>
<cloudcover>75</cloudcover>
<FeelsLikeC>16</FeelsLikeC>
<FeelsLikeF>61</FeelsLikeF>
</current_condition>
<weather>
<date>2014-04-11</date>
<astronomy>
<sunrise>07:08 AM</sunrise>
<sunset>08:36 PM</sunset>
<moonrise>04:45 PM</moonrise>
<moonset>05:15 AM</moonset>
</astronomy>
</weather>
<data>
在我出错的地方提供任何帮助都将不胜感激,因为我使用了许多变体,但似乎无法使其正确
谢谢
---------------------------------代码编辑---------------------------
我已经设法让代码以我想要的方式工作,不确定它是否正确,但这就是我所做的
为类创建了构造函数,如下所示:
public DisplayWeatherCurrentConditions(DisplayWeatherAstronomy dwa)
{
SunRise = dwa.SunRise;
Sunset = dwa.SunSet;
}
public string SunRise { get; set; }
public string Sunset { get; set; }
然后将代码更改为
var displayAll = (from wd in result.Descendants("current_condition")
from ts in result.Descendants("astronomy")
终于可以在天文学课上添加属性了
SunRise = (string)ts.Element("sunrise") ?? string.Empty,
Sunset = (string)ts.Element("sunset") ?? string.Empty,
如果有比我更有经验的人可以改进这一点,请删除
。子体(“数据”)
使用结果。子体(“当前状况”)
,而且我在当前状况
中没有看到任何天气
元素,如果要获取以weather
开头的元素,则需要使用
result.Descendants("current_condition")
.Descendants()
.Where(x => x.Name.ToString().StartsWith("weather"))
或者,如果您只想获取weather
元素,请使用结果。子体(“weather”)
无效的xml块它不完整请格式化您的xml以便更好地阅读
没有子体
。的结束标记在哪里?这就是您的全部xml吗?我想您误解了SelectMany的意思。感谢您的回复,我已经返回了当前条件,但正在与天文学斗争,因为返回了0。我会继续努力的
result.Descendants("current_condition")
.Descendants()
.Where(x => x.Name.ToString().StartsWith("weather"))