Having problems with XML



该代码位于下面,旨在从Internet中提取XML文件并在下面解析。但是,当我尝试从此XML文件中提取值时,引起异常(NullReferance Exeption)。

这是代码,已注意到该错误发生在哪里:

代码下方是我试图从中提取值的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与XML文档的重复,因为已经对代码进行了重大更改。

您似乎没有检查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

没有其他人可以正确调试您的代码,因为您正在调用我们无法确定的URL(您具有未知变量latitudelongitude

我已经调试了xDoc.Root.Element("forecast").Element("pop").Value行,其中XDOC是根据您给出的长XML文本创建的,也不例外。这样我就可以建议以下内容。

  1. 确保n.Result是您认为的。将其记录在某个地方以确保它不是空白的字符串,JSON字符串或其他任何意外的。

  2. 尝试确切找出零引用异常发生的位置。为此,请解开所有这些方法调用,并且每行有一个方法调用:

    var root = xDoc.Root;
    var forecast = root.Element("forecast");
    var pop = forecast.Element("pop");
    var popValue = pop.Value;
    tempnow.Text = popValue;
    

那么至少您会确切地知道例外来自哪里。

最新更新