Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As part of an application I am making to display weather and forecasts to the user through the Met Office' DataPoint service I have to read the files containing the weather data which are in XML format. I have been trying for days to do this and have had no success so I thought I would ask here. The XML file I need to get specific data from is this:

XML
<Layers type="Observation">
    <BaseUrl forServiceTimeFormat="Times">       
    <!--URL goes here-->
    </BaseUrl>
    <Layer displayName="Lightning">
        <Service name="OBSERVATIONS">
            <LayerName>ATDNET_Sferics</LayerName>
            <ImageFormat>png</ImageFormat>
            <Times>
                <Time>2013-08-06T13:45:00</Time>
                <Time>2013-08-06T13:30:00</Time>
                <Time>2013-08-06T13:15:00</Time>
                <Time>2013-08-06T13:00:00</Time>
                <Time>2013-08-06T12:45:00</Time>
                <Time>2013-08-06T12:30:00</Time>
                <Time>2013-08-06T12:15:00</Time>
                <Time>2013-08-06T12:00:00</Time>
                <Time>2013-08-06T11:45:00</Time>
                <Time>2013-08-06T11:30:00</Time>
                <Time>2013-08-06T11:15:00</Time>
                <Time>2013-08-06T11:00:00</Time>
                <Time>2013-08-06T10:45:00</Time>
            </Times>
        </Service>
    </Layer>
    <Layer displayName="SatelliteIR">
        <Service name="OBSERVATIONS">
            <LayerName>SATELLITE_Infrared_Fulldisk</LayerName>
            <ImageFormat>png</ImageFormat>
            <Times>
                <Time>2013-08-06T12:00:00</Time>
                <Time>2013-08-06T09:00:00</Time>
                <Time>2013-08-06T06:00:00</Time>
                <Time>2013-08-06T03:00:00</Time>
                <Time>2013-08-06T00:00:00</Time>
                <Time>2013-08-05T21:00:00</Time>
                <Time>2013-08-05T18:00:00</Time>
                <Time>2013-08-05T15:00:00</Time>
                <Time>2013-08-05T12:00:00</Time>
            </Times>
        </Service>
    </Layer>
    <Layer displayName="SatelliteVis">
        <Service name="OBSERVATIONS">
            <LayerName>SATELLITE_Visible_N_Section</LayerName>
            <ImageFormat>png</ImageFormat>
            <Times>
                <Time>2013-08-06T12:00:00</Time>
                <Time>2013-08-06T09:00:00</Time>
                <Time>2013-08-06T06:00:00</Time>
                <Time>2013-08-06T03:00:00</Time>
                <Time>2013-08-06T00:00:00</Time>
                <Time>2013-08-05T21:00:00</Time>
                <Time>2013-08-05T18:00:00</Time>
                <Time>2013-08-05T15:00:00</Time>
                <Time>2013-08-05T12:00:00</Time>
            </Times>
        </Service>
    </Layer>
    <Layer displayName="Rainfall">
        <Service name="OBSERVATIONS">
            <LayerName>RADAR_UK_Composite_Highres</LayerName>
            <ImageFormat>png</ImageFormat>
            <Times>
                <Time>2013-08-06T13:45:00</Time>
                <Time>2013-08-06T13:30:00</Time>
                <Time>2013-08-06T13:15:00</Time>
                <Time>2013-08-06T13:00:00</Time>
                <Time>2013-08-06T12:45:00</Time>
                <Time>2013-08-06T12:30:00</Time>
                <Time>2013-08-06T12:15:00</Time>
                <Time>2013-08-06T12:00:00</Time>
                <Time>2013-08-06T11:45:00</Time>
                <Time>2013-08-06T11:30:00</Time>
                <Time>2013-08-06T11:15:00</Time>
                <Time>2013-08-06T11:00:00</Time>
                <Time>2013-08-06T10:45:00</Time>
            </Times>
        </Service>
    </Layer>
</Layers>


So my question is how would I read the XML file and put the list of the 9 times for specifically the SatelliteVis layer/section into a comboBox for the user to select a time step from those 9 that I would read from the XML? I am writing in C# using Microsoft Visual C# 2010. I would really appreciate any help you can give me.
Posted

In .NET FCL, there are different XML parsers. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


Enjoy,
—SA
 
Share this answer
 
Comments
ridoy 6-Aug-13 12:41pm    
a plenty,+5 SA.
Sergey Alexandrovich Kryukov 6-Aug-13 12:42pm    
Thank you very much,
—SA
I prefer to use XPath to get the list of Time nodes.
Try this code:
C#
Xml.XmlNodeList xmlNodes;
Xml.XmlDocument xmlDoc = New Xml.XmlDocument();
xmlDoc.LoadXml(strXML); //strXML is a var that contains your xml (that you posted, above)

const string xpathQuery = "/Layers/Layer[@displayName='SatelliteVis']/Service/Times/Time";
xmlNodes = xmlDoc.SelectNodes(xpathQuery);

//iterate through the list of nodes and load your listbox
for (int x = 0; x < xmlNodes.Count; x++)
{
    listbox1.add(xmlNodes(x).InnerText);
}
 
Share this answer
 
v2
Comments
Henry Hunt 6-Aug-13 12:44pm    
Thank you so much, it worked great after a few changes. One more thing, it returns the whole element (e.g: 'time 2013-08-06T15:00:00 /time' is there a way to just get the data inside the tags or would I just use replace to replace the 2 tags either side of the data with an empty space?
Tim Golisch 6-Aug-13 15:19pm    
I changed the code (above) to use xmlNodes(x).InnerText instead of xmlNodes(x).OuterXml. That should be more useful for you.
Henry Hunt 7-Aug-13 6:53am    
Another thing, how would I read an attribute and put it into a string for use in a URL? For example, how would I read the attribute value for 'displayName' in the SatelliteVis layer and store it in a string? Thanks.
Tim Golisch 7-Aug-13 8:24am    
You can use this XPath query to get the list of displayName values:
"/Layers/Layer/@displayName"
Henry Hunt 8-Sep-13 12:08pm    
Hello again, I have just one more thing for you. How would I reference to a specific 'set' of elements depending on on of the containing element values instead of the attribute at the top of the 'set' of elements. For example: The code I want to read is below. How would I get the Risk level from specifying the Area element?

How would I get the risk level for the Area value of East Highland? I don't want to use indexing, ie: using SelectNodes("")[n].innerText as the 'sets' of MountainForecast elements could be in a different order Thank you.


mountainforecastlist
mountainforecast
datadate/2013-09-08T04:04:39Z/datadate
validfrom/2013-09-08T04:00:00Z/validfrom
validto/2013-09-12T04:00:00Z/validto
createddate/2013-09-08T03:05:48Z/createddate
uri/http://datapoint.metoffice.gov.uk/public/data/txt/wxfcs/mountainarea/{format}/100?key={key}/uri
Area/Brecon Beacons/Area
risk/Medium/risk
mountainforecast
mountainforecast
datadate/2013-09-08T03:26:42Z/datadate
validfrom/2013-09-08T03:00:00Z/validfrom
validto/2013-09-12T03:00:00Z/validto
createddate/2013-09-08T02:26:48Z/createddate
uri/http://datapoint.metoffice.gov.uk/public/data/txt/wxfcs/mountainarea/{format}/101?key={key}/uri
Area/East Highland/Area
risk/Medium/risk
mountainforecast
mountainforecastlist

Had to remove the tag start and ends so hope you can read it, I put / between element and its value.

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