NullReferenceException与XML文档 [英] NullReferenceException with XML Document
问题描述
运行此行代码时,我收到System.NullReferenceException.
When this line of code is run, I receive a System.NullReferenceException.
// 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是在该类下定义的(如果与此相关).
Xdoc is defined below the class, if this is related to it.
{
public partial class MainPage : PhoneApplicationPage
{
XDocument xDoc;
这是我试图访问并找到"record"值的XML文档.这也是"Xdoc"代表的文档,有关以下内容.
This is the XML document I am attempting to access and find the value of "record". This is also the document "Xdoc" represents, with information about that below.
<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代表上面的文档,该文档是从Internet下载的:
How I define that Xdoc represents the document above, downloaded from the internet:
//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);
推荐答案
xDoc.Root
已经是<data>
元素,因此您不必再次调用.Element("data")
.
xDoc.Root
is already <data>
element, so you don't have to call .Element("data")
again.
以下应该可以正常工作:
Following should work just fine:
tempnow.Text = xDoc.Root.Element("forecast").Element("pop").Value;
但是,我宁愿使用(string)XElement
强制转换:
however, I'd rather use (string)XElement
casting:
tempnow.Text = (string)xDoc.Root.Element("forecast").Element("pop");
这篇关于NullReferenceException与XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!