我正在使用> http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl webservice通过调用GmlTimeSeries webmethod来获取天气细节。现在我只想从xml中读取温度、天气图标链接的详细信息。xml拥有巨大的数据。有谁能给出一个从xml中获取所需数据的想法吗?
NDFD HOme Page
XML看起来像下面这样:Full XML File is Here
我想从下面的xml数据中获取温度:
<gml:featureMember>
<app:Forecast_Gml2Point>
<gml:position>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
</gml:Point>
</gml:position>
<app:validTime>2011-06-07T12:00:00</app:validTime>
<app:temperature>77.0</app:temperature>
</app:Forecast_Gml2Point>
</gml:featureMember>
<gml:featureMember>
<app:Forecast_Gml2Point>
<gml:position>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
</gml:Point>
</gml:position>
<app:validTime>2011-06-07T15:00:00</app:validTime>
<app:temperature>90.0</app:temperature>
</app:Forecast_Gml2Point>
</gml:featureMember>以及下面的天气短语:
<gml:featureMember>
<app:Forecast_Gml2Point>
<gml:position>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
</gml:Point>
</gml:position>
<app:validTime>2011-06-08T03:00:00</app:validTime>
<app:weatherPhrase>Mostly Clear</app:weatherPhrase>
</app:Forecast_Gml2Point>
</gml:featureMember>
<gml:featureMember>
<app:Forecast_Gml2Point>
<gml:position>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
</gml:Point>
</gml:position>
<app:validTime>2011-06-08T06:00:00</app:validTime>
<app:weatherPhrase>Mostly Clear</app:weatherPhrase>
</app:Forecast_Gml2Point>
</gml:featureMember>上面是一段xml文件。像这样,我有7天天气细节的大量数据。我需要从上面的xml中读取温度和天气情况。
Full XML File is Here
发布于 2011-06-07 19:55:16
我想你会找到你的答案here
编辑:需要使用命名空间,例如:
XNamespace app = "http://www.weather.gov/forecasts/xml/OGC_services";
var result = from i in doc.Descendants(app+"Forecast_Gml2Point")
select new
{
temperature = i.Element(app + "temperature"),
icon = i.Element(app+"weatherIcon")
};编辑2:如果你需要获取其他命名空间的元素,下面是另一个例子:
XNamespace gml ="http://www.opengis.net/gml"
i.Element(gml+"coordinates" )发布于 2011-06-07 21:18:21
如果您使用Visual Studio的“添加Web引用”功能,这将会更容易。这样,Visual Studio将基于WSDL为您生成所有(代理)类,然后您可以像往常一样对这些类进行编程。换句话说,不需要解析XML。
正如在this link中指出的那样
是在客户端上创建的代理类,用于连接到服务器上运行的Studio.Net。在IDE中,Web引用会自动生成代码并将隐藏文件插入到项目中。这是必需的,因为.Net是类型安全的,并且为了编译使用Web Service的代码,客户端必须知道所调用的每个方法的方法签名。
有关使用WSDL的详细信息,请参阅the above link。
https://stackoverflow.com/questions/6264196
复制相似问题