0

I am trying to simply read the value of a few elements and attributes in an XML document but I cannot get it to work. My XML file is below:

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

And this is the code I am using to read the value of the UK outlook for 28 Aug - 11 Sept:

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

As you can see I have researched namespaces when using XPath and tried it but unless I have done it wrong I cannot get it to work. I have been reading XML files for ages but don't know why this is throwing an exception. All I want to do is read the value of that attribute. Thanks

2 Answers 2

1

All of the elements are inside the same namespace and so you should apply the same namespace prefix to all of them:

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

Alternatively, set www.metoffice.gov.uk/xml/metoRegionalFcst as the default namespace by using string.Empty instead of "ns" in your AddNamespace call and just avoid the use of any prefixes.

Sign up to request clarification or add additional context in comments.

Comments

0

Your XML has default namespace, so the element where the default namespace declared and all of it's descendants without prefix and without different default namespace declared are considered within the same namespace.

Use the same prefix for all elements in your XPath :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.