我以xml格式消费openweathermap API。在此XML数据节点中嵌套和无序。我不知道如何在转发器控件中绑定数据。
这是我的C#代码
using System.Net;
protected void GetWeatherInfo()
{
string appId = "API KEY";
string url = string.Format("http://api.openweathermap.org/data/2.5/forecast/daily?q=London&units=metric&mode=xml&cnt=2&APPID={0}", appId);
using (WebClient client = new WebClient())
{
string xmlData= client.DownloadString(url);
RepDetails.DataSource = xmlData;
RepDetails.DataBind();
}
}
来自网址的数据
<weatherdata>
- <location>
<name>London</name>
<type />
<country>GB</country>
<timezone />
<location altitude="0" latitude="51.50853" longitude="-0.12574" geobase="geonames" geobaseid="2643743" />
</location>
<credit />
- <meta>
<lastupdate />
<calctime>0.0278</calctime>
<nextupdate />
</meta>
<sun rise="2015-12-16T08:00:32" set="2015-12-16T15:51:44" />
- <forecast>
- <time day="2015-12-16">
<symbol number="500" name="light rain" var="10d" />
<precipitation value="1.22" type="rain" />
<windDirection deg="250" code="WSW" name="West-southwest" />
<windSpeed mps="8.54" name="Fresh Breeze" />
<temperature day="13.11" min="12.09" max="13.2" night="12.22" eve="13.02" morn="12.63" />
<pressure unit="hPa" value="1023.73" />
<humidity value="94" unit="%" />
<clouds value="broken clouds" all="64" unit="%" />
</time>
- <time day="2015-12-17">
<symbol number="500" name="light rain" var="10d" />
<precipitation value="0.5" type="rain" />
<windDirection deg="199" code="SSW" name="South-southwest" />
<windSpeed mps="7.17" name="Moderate breeze" />
<temperature day="12.86" min="11.16" max="12.87" night="11.16" eve="12.48" morn="11.61" />
<pressure unit="hPa" value="1019.9" />
<humidity value="89" unit="%" />
<clouds value="broken clouds" all="68" unit="%" />
</time>
</forecast>
</weatherdata>
所以,根据我的要求,我需要sun和time节点数据,但我不知道如何在转发器控制中管理父节点数据和子节点数据之间的关系。
<itemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("sun.rise") %>' Font-Bold="true" />
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("time.temprature.day") %>' />
</itemTemplate>