在读取XML文件时需要帮助 [英] Need help with reading XML files

查看:146
本文介绍了在读取XML文件时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为应用程序的一部分,我要通过Met Office的DataPoint服务向用户显示天气和天气预报,我必须读取包含XML格式的天气数据的文件.我已经尝试了好几天了,但是没有成功,所以我想在这里问一下.我需要从中获取特定数据的XML文件是这样的:

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:

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



所以我的问题是我将如何读取XML文件并将9个特定的SatelliteVis图层/部分的列表放到comboBox中,以便用户从我要从XML读取的那9个时间步中选择一个时间步长?我正在使用Microsoft Visual C#2010用C#编写.非常感谢您能提供给我的任何帮助.



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.

推荐答案

在.NET FCL中,是不同的XML解析器.这是我对它们的简短概述:

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

  1. 使用System.Xml.XmlDocument类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
    请参见
  2. 使用类System.Xml.XmlTextReader; library/system.xml.xmldocument.aspx"target =" _ blank"title =" New Window> ^ ].
  3. 使用类System.Xml.XmlTextReader;这是最快的读取方法,尤其是您需要跳过一些数据.
    请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [
  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[^].



享受,

—SA



Enjoy,

—SA


我更喜欢使用XPath来获取Time节点的列表.
尝试以下代码:
I prefer to use XPath to get the list of Time nodes.
Try this code:
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);
}


这篇关于在读取XML文件时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆