Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read an XML file from online but it has a namespace at the start which I have not come across before and don't know how to read the file. My Xpath works without the namespace and I have looked online and found that you need to use a namespace manager. I tried that but the namespace does not have a prefix which you need to specify a namespace so I don't really know what to do now. Could someone please help me get this code to work:
C#
Console.WriteLine("This program displays a forecast for a region in the UK");
            Console.Write("Enter region ID: ");
            string s = Console.ReadLine();
            while (!Regex.IsMatch(s,"[5-5][0-1][0-6]"))
            {
                Console.Write("Enter region ID: ");
                s = Console.ReadLine();
            }
            Console.WriteLine("");

            XmlDocument forecast = new XmlDocument();            
            forecast.Load("http://datapoint.metoffice.gov.uk/public/data/txt/wxfcs/regionalforecast/xml/" + s + "?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");            
            Console.WriteLine("----- Information -----------------");
            Console.WriteLine("Created : " + forecast.SelectSingleNode("//@createdOn").InnerText);
            Console.WriteLine("Issued  : " + forecast.SelectSingleNode("//@issuedAt").InnerText);
            Console.WriteLine("----- Forecast --------------------");
            Console.WriteLine("Headline : " + forecast.SelectSingleNode("//Period[@id='day1to2']/Paragraph[@title='Headline:']").InnerText);

            Console.ReadKey();

...To read this XML file
XML
<RegionalFcst xmlns="www.metoffice.gov.uk/xml/metoRegionalFcst" createdOn="2013-10-31T03:32:02" issuedAt="2013-10-31T04:00:00" regionId="wm">
<FcstPeriods>
<Period id="day1to2">
<Paragraph title="Headline:">
A bright start but scattered blustery showers later.
</Paragraph>
<Paragraph title="Today:">
Some bright or sunny spells this morning and becoming breezy. Scattered showers will spread from the west by midday, becoming increasingly frequent through the afternoon and occasionally heavy. Maximum Temperature 13C.
</Paragraph>
<Paragraph title="Tonight:">
A mostly dry night with clear spells, lighter winds and a few mist patches. Cloud increasing during the early hours though with rain possible in southern counties. Minimum Temperature 6C.
</Paragraph>
<Paragraph title="Friday:">
Bright spells possible at first but soon turning cloudy with outbreaks of rain spreading north, perhaps more persistent and heavy later in the afternoon. Winds will stay light. Maximum Temperature 13C.
</Paragraph>
</Period>
<Period id="day3to5">
<Paragraph title="Outlook for Saturday to Monday:">
Strong winds over the weekend with bright spells and showery outbreaks of rain, heaviest on Saturday. Unsettled on Monday with more persistent rain likely.
</Paragraph>
</Period>
<Period id="day6to15">
<Paragraph title="UK Outlook for Tuesday 5 Nov 2013 to Thursday 14 Nov 2013:">
Cloud and rain expected to quickly spread eastwards across the UK on Tuesday, the heaviest rain in the northwest, where gales are likely. On Wednesday, further cloud and rain in the northwest, with occasional rain but also some drier, brighter interludes towards the south and east. Turning milder, especially in the south and east. Largely unsettled conditions thereafter with showers or longer spells of rain, locally heavy, particularly in the northwest, where it will also be windy at times. There will, however, be some drier and brighter interludes, the best of these in the south and east. Remaining generally mild for the time of year. Later in the period, these more settled conditions may last longer in the south resulting in chilly nights here, with more overnight fog.
</Paragraph>
</Period>
<Period id="day16to30">
<Paragraph title="UK Outlook for Friday 15 Nov 2013 to Friday 29 Nov 2013:">
Early November's mostly unsettled conditions across northern and western regions are thought likely to persist through much of the rest of the month. As such, rainfall amounts here are more likely than not to be above average and conditions may also be quite windy at times. Further south and east, rainfall accumulations are considered likely to be nearer average, implying slightly more settled conditions with drier, brighter periods interspersed with occasional bouts of unsettled weather. Drier periods here may, however, be accompanied by overnight and morning fog patches. Taking the country as a whole, temperatures during this period are more likely than not to be near, or perhaps above average throughout, leading to a lower risk of overnight frost than can usually be expected at this time of year.
</Paragraph>
</Period>
</FcstPeriods>
</RegionalFcst>
Posted

That namespace with no prefix is the default namespace. Just because they didn't give it a prefix doesn't mean you can't give it one in your NamespaceManager, though:
C#
System.Xml.XmlNamespaceManager nsm = new System.Xml.XmlNamespaceManager(forecast.NameTable);
nsm.AddNamespace("x", "www.metoffice.gov.uk/xml/metoRegionalFcst");
MessageBox.Show(forecast.SelectSingleNode("//x:Period[@id='day1to2']/x:Paragraph[@title='Headline:']", nsm).OuterXml);
 
Share this answer
 
Have a look into the below article. It should be fairly simple with Linq to XML

http://blogs.msdn.com/b/wriju/archive/2008/05/29/linq-to-xml-querying-xml-with-namespaces.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900