XPath是正确的,使用NamespaceManager但仍然得到NullReferenceException

时间:2014-08-15 09:01:38

标签: c# xpath

我试图简单地读取XML文档中的一些元素和属性的值,但我无法让它工作。我的XML文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<RegionalFcst xmlns="www.metoffice.gov.uk/xml/metoRegionalFcst" createdOn="2014-08-13T16:15:24" issuedAt="2014-08-13T16:00:00" regionId="os">
  <FcstPeriods>
    <Period id="day1to2">
      <Paragraph title="Headline:">Showers continuing but winds easing, drier and brighter on Friday.</Paragraph>
      <Paragraph title="This Evening and Tonight:">Rather cloudy and scattered showers will continue, some briefly heavy, though a few late glimpses of sun possible this evening. Further showers tonight. Fresh North or Northwest winds swill slowly ease. Minimum Temperature 10C.</Paragraph>
      <Paragraph title="Thursday:">A showery day but with some pleasant bright or sunny intervals. Moderate locally fresh Northerly winds easing later. Maximum Temperature 15C.</Paragraph>
    </Period>
    <Period id="day3to5">
      <Paragraph title="Outlook for Friday to Sunday:">Friday will become mostly dry with some pleasant sunny spells. However rain will spread from the west on Friday night and leave a cloudy rainy weekend. Windy with gales later.</Paragraph>
    </Period>
    <Period id="day6to15">
      <Paragraph title="UK Outlook for Monday 18 Aug 2014 to Wednesday 27 Aug 2014:">Any early rain in the south will soon clear to leave sunshine and showers on Monday and Tuesday, these heaviest and most frequent in the north and northeast. Rather windy with the risk of gales along North Sea coasts. Temperatures on the cool side for the time of year with chilly nights. Unsettled conditions will persist into the middle of next week with many areas seeing further showers. However, towards the weekend, the showers should become lighter and less frequent with southern and southwestern areas probably turning mostly dry. Looking ahead to the latter part of the forecast period, further showers or rain remain likely in the north where temperatures will be cool at times, whilst the best of the brighter and, subsequently, warmer conditions will be in the south.</Paragraph>
    </Period>
    <Period id="day16to30">
      <Paragraph title="UK Outlook for Thursday 28 Aug 2014 to Thursday 11 Sep 2014:">Current signals continue to suggest a broadly changeable spell of weather. As such, most regions can expect to see spells of fine weather, with some warm sunshine at times. However, these spells will then be interspersed with more unsettled conditions bringing showery outbreaks and perhaps more prolonged spells of rain. Northern and western parts are then likely to see the most frequent bouts of unsettled weather, whilst southern and eastern parts are likely to see the most frequent and prolonged fine and dry spells. Daytime temperatures, meanwhile, are likely to be warm during fine spells and near or below average during unsettled weather.</Paragraph>
    </Period>
  </FcstPeriods>
</RegionalFcst>

这是我用来阅读8月28日 - 9月11日英国展望价值的代码:

XmlDocument datas = new XmlDocument();
datas.Load(Path.Combine(Profile.Cache, "RTxtFcs", "RTxtFcs" + siteId + ".xml"));
XmlNamespaceManager manager = new XmlNamespaceManager(datas.NameTable);
manager.AddNamespace("ns", "www.metoffice.gov.uk/xml/metoRegionalFcst");

//day1To2A.Content = datas.SelectNodes("//FcstPeriods/Period[@id=day1to2]/Paragraph")[1].InnerText;

Label label = new Label();
label.Content = datas.SelectSingleNode("/ns:RegionalFcst/FcstPeriods/Period[@id='day16to30']/Paragraph/@title", manager).InnerText;
label.SetValue(Label.FontWeightProperty, FontWeights.Bold);
stack.Children.Add(label);

正如您所看到的,我在使用XPath时已经研究了名称空间并尝试了它,但除非我做错了否则我无法使其工作。我已经阅读了多年的XML文件,但不知道为什么会抛出异常。我想要做的就是读取该属性的值。感谢

2 个答案:

答案 0 :(得分:1)

所有元素都在同一名称空间内,因此您应该为所有元素应用相同的名称空间前缀:

/ns:RegionalFcst/ns:FcstPeriods/ns:Period[@id='day16to30']/ns:Paragraph/@title

或者,在www.metoffice.gov.uk/xml/metoRegionalFcst来电中使用string.Empty代替"ns",将AddNamespace设置为默认命名空间,只需避免使用任何前缀。

答案 1 :(得分:0)

您的XML具有默认命名空间,因此默认命名空间声明的元素及其所有没有前缀且没有声明不同默认命名空间的后代都被视为在同一命名空间内。

对XPath中的所有元素使用相同的前缀:

/ns:RegionalFcst/ns:FcstPeriods/ns:Period[@id='day16to30']/ns:Paragraph/@title