添加使用的XmlNamespaceManager命名空间在C# [英] add namespace using xmlnamespacemanager in C#

查看:340
本文介绍了添加使用的XmlNamespaceManager命名空间在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读<一个数据href=\"http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLclient.php?whichClient=GmlLatLonList&lat=&lon=&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2Lat=&listEndPoint2Lon=&zip$c$cList=&listZip$c$cList=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=&sector=&gmlListLatLon=38.99,-77.02%2039.70,-104.80%2047.6,-122.30&featureType=Forecast_Gml2Point&requestedTime=&startTime=2000-01-01T00:00:00&endTime=2012-01-01T00:00:00&compType=Between&propertyName=wx,temp,icons&product=time-series&begin=2004-01-01T00:00:00&end=2015-06-07T00:00:00&maxt=maxt&Submit=Submit\">XML文件。在此元素与应用和GML文本pfixed $ P $。因为这些prefixes的我无法读取数据。对于此,我尝试使用XmlNamespaceManager的,但没有得到补充命名空间。

I am trying to read the data from XML File. In this elements are prefixed with 'app' and 'gml' text. because of these prefixes I am unable to read the data. For this I am trying to add namespace by using XMLNamespaceManager but not getting.

编辑:

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(new StringReader(strResult));
        NameTable nt = new NameTable();
        XmlNamespaceManager prefix = new XmlNamespaceManager(nt);
        string nmspc = xmlDoc.DocumentElement.NamespaceURI;
        prefix.AddNamespace("app:",xmlDoc.DocumentElement.NamespaceURI);

        prefix.PushScope();

下面的strResult包含<一个href=\"http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLclient.php?whichClient=GmlLatLonList&lat=&lon=&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2Lat=&listEndPoint2Lon=&zip$c$cList=&listZip$c$cList=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=&sector=&gmlListLatLon=38.99,-77.02%2039.70,-104.80%2047.6,-122.30&featureType=Forecast_Gml2Point&requestedTime=&startTime=2000-01-01T00:00:00&endTime=2012-01-01T00:00:00&compType=Between&propertyName=wx,temp,icons&product=time-series&begin=2004-01-01T00:00:00&end=2015-06-07T00:00:00&maxt=maxt&Submit=Submit\">XML数据

Here the strResult contains XML Data

推荐答案

是这样的:

var doc = new XmlDocument();
doc.Load(source);
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("app", "http://www.weather.gov/forecasts/xml/OGC_services");
var firstPoint = doc.SelectSingleNode("//app:Forecast_Gml2Point", nsmgr);

请注意,该命名空间的别名仅仅是便利,并且不具有对文件和命名空间管理器之间的配合 - 但如果他们这样做可能更容易

Note that the namespace aliases are merely conveniences, and don't have to match between the document and the namespace-manager - but it is probably easier if they do.

这篇关于添加使用的XmlNamespaceManager命名空间在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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