此代码位于下方,旨在从Internet中提取XML文件并在下面进行解析。但是,当我尝试从此XML文件中提取值时,会导致异常(NullReferanceExeption)。
这是代码,已经注明了发生错误的行:
代码下面是我试图从中提取值的XML文件。
//Create the forecast
void makeforecast()
{
//Make Webclient Request
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri("http://api.wt360.com/data/forecast/daily-4day?key=ic1uji3s9zjnjgcd6hq64ny6alkoin9119vjrxc1wwngdzuihk&l=" + latitude + "," + longitude + "&fmt=xml"));
client.DownloadStringCompleted += (s, n) =>
{
progress.Visibility = System.Windows.Visibility.Collapsed;
((Storyboard)this.Resources["trans"]).Begin();
XDocument xDoc = XDocument.Parse(n.Result);
//Current Conditions
if (debug2.Text == "Temp")
{
}
if (debug2.Text == "Sun")
{
}
if (debug2.Text == "Rain")
{
THIS LINE CAUSES THE EXEPTION >> tempnow.Text = xDoc.Root.Element("forecast").Element("pop").Value;**
}
}
}
和XML:
<data>
<status>success</status>
<location>
<city/>
<fullName>39.6N, 0.3W - 6miles SW of El Puerto, SP</fullName>
<localDate>2014-01-30T01:29:23</localDate>
<localEpoch>1391045363</localEpoch>
<locationID>GP202368</locationID>
</location>
<forecast>
<record>0</record>
<utcEpoch>1391040000</utcEpoch>
<utcDate>2014-01-30T00:00:00</utcDate>
<maxTempC>15.4</maxTempC>
<maxTempF>59.7</maxTempF>
<minTempC>5.2</minTempC>
<minTempF>41.3</minTempF>
<fcstDay>1</fcstDay>
<dayLength>10:07</dayLength>
<sunriseHHMMLocal>8:11am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:18pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>19</pop>
<uv_index>2</uv_index>
<gustC>30</gustC>
<gustF>19</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>partly-cloudy-sm.png</icon>
<iconBase>partly-cloudy</iconBase>
<iconLg>partly-cloudy.png</iconLg>
<prcpC>0</prcpC>
<prcpF>0</prcpF>
<rh>33</rh>
<skyCover>8</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>NW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>16</wspdC>
<wspdF>10</wspdF>
<wx>Mostly Sunny</wx>
</forecast>
<forecast>
<record>1</record>
<utcEpoch>1391126400</utcEpoch>
<utcDate>2014-01-31T00:00:00</utcDate>
<maxTempC>16</maxTempC>
<maxTempF>60.8</maxTempF>
<minTempC>5</minTempC>
<minTempF>40.9</minTempF>
<fcstDay>2</fcstDay>
<dayLength>10:09</dayLength>
<sunriseHHMMLocal>8:10am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:19pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>18</pop>
<uv_index>2</uv_index>
<gustC>30</gustC>
<gustF>19</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>partly-cloudy-sm.png</icon>
<iconBase>partly-cloudy</iconBase>
<iconLg>partly-cloudy.png</iconLg>
<prcpC>0</prcpC>
<prcpF>0</prcpF>
<rh>46</rh>
<skyCover>28</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>WNW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>13</wspdC>
<wspdF>8</wspdF>
<wx>Mostly Sunny</wx>
</forecast>
<forecast>
<record>2</record>
<utcEpoch>1391212800</utcEpoch>
<utcDate>2014-02-01T00:00:00</utcDate>
<maxTempC>18.1</maxTempC>
<maxTempF>64.5</maxTempF>
<minTempC>6.4</minTempC>
<minTempF>43.5</minTempF>
<fcstDay>3</fcstDay>
<dayLength>10:12</dayLength>
<sunriseHHMMLocal>8:09am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:21pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>46</pop>
<uv_index>2</uv_index>
<gustC>49</gustC>
<gustF>31</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>light-rain-shower-sm.png</icon>
<iconBase>light-rain-shower</iconBase>
<iconLg>light-rain-shower.png</iconLg>
<prcpC>0.2</prcpC>
<prcpF>0.09</prcpF>
<rh>35</rh>
<skyCover>27</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>WNW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>22</wspdC>
<wspdF>14</wspdF>
<wx>Light Rain Showers</wx>
</forecast>
<forecast>
<record>3</record>
<utcEpoch>1391299200</utcEpoch>
<utcDate>2014-02-02T00:00:00</utcDate>
<maxTempC>15.9</maxTempC>
<maxTempF>60.7</maxTempF>
<minTempC>4.2</minTempC>
<minTempF>39.6</minTempF>
<fcstDay>4</fcstDay>
<dayLength>10:14</dayLength>
<sunriseHHMMLocal>8:08am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:22pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>12</pop>
<uv_index>2</uv_index>
<gustC>41</gustC>
<gustF>26</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>sunny-sm.png</icon>
<iconBase>sunny</iconBase>
<iconLg>sunny.png</iconLg>
<prcpC>0</prcpC>
<prcpF>0</prcpF>
<rh>34</rh>
<skyCover>0</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>WNW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>19</wspdC>
<wspdF>12</wspdF>
<wx>Sunny</wx>
</forecast>
</data>
请不要将此作为NullReferenceException with XML Document的副本链接,因为对代码进行了重大更改。
答案 0 :(得分:0)
您似乎没有检查XML是否符合您的预期。例如,当我运行你的代码时,我得到以下XML:
<?xml version="1.0" encoding="utf-8"?>
<data>
<status>error</status>
<errorCode>00001</errorCode>
<errorMessage>Request quota has been exceeded. Please contact support.</errorMessage><errorMethod></errorMethod>
<errorModule>api</errorModule>
</data>
您需要先检查状态元素。
if(xDoc.Root.Element("status").ToString().ToLower() == "success")
//run your code
答案 1 :(得分:0)
没有其他人可以正确调试您的代码,因为您正在调用我们无法确定的网址(您有未知变量latitude
和longitude
。
我调试了行xDoc.Root.Element("forecast").Element("pop").Value
,其中xDoc是根据您提供的长xml文本创建的,并且没有异常。所以我可以建议如下。
确保n.Result
符合您的想法。将其记录在某处以确保它不是空字符串,或json字符串,或其他任何意外的。
尝试找出空引用异常的确切位置。为此,取消所有这些方法调用,并每行调用一次方法:
var root = xDoc.Root;
var forecast = root.Element("forecast");
var pop = forecast.Element("pop");
var popValue = pop.Value;
tempnow.Text = popValue;
然后至少你会确切知道异常的来源。