2014-01-30 91 views
1

當這行代碼運行時,我收到一個System.NullReferenceException。NullReferenceException與XML文檔

// Occurs on this line 
if (xDoc.Root.Element("data").Element("forecast").Element("record").Value == "0") 
{ 
    tempnow.Text = xDoc.Root.Element("data").Element("forecast").Element("pop").Value; 
} 

Xdoc定義在類的下面,如果這與它有關。

{ 
    public partial class MainPage : PhoneApplicationPage 
    { 

     XDocument xDoc; 

這是我試圖訪問的XML文檔並找到「record」的值。這也是「Xdoc」所代表的文件,其中包含以下信息。

<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> 

我如何定義代表XDOC文檔上面,從網上下載:

//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) => 
//^This is in a different method then the code receiving the NullReferenceException. 
xDoc = XDocument.Parse(n.Result); 

回答

5

xDoc.Root已經是<data>元素,讓你不必再打電話.Element("data")做。

以下應該只是罰款:

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

不過,我寧願使用(string)XElement鑄造:

tempnow.Text = (string)xDoc.Root.Element("forecast").Element("pop"); 
+0

出於某種原因,仍然得到同樣的錯誤。這是用你的awnser修改的代碼:'if(xDoc.Root.Element(「forecast」)。Element(「pop」)。Value ==「0」){ tempnow.Text =(string)xDoc.Root。元素(「預測」)元素(「彈出」)。 }' – Kevin