我正在尝试从XML File读取数据。在这个元素中加上'app'和'gml'文本的前缀。因为这些前缀我无法读取数据。为此,我试图通过使用XMLNamespaceManager添加命名空间,但没有得到。
编辑:
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包含XML Data
答案 0 :(得分:15)
类似的东西:
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);
请注意,命名空间 别名 仅仅是方便,而且不必在文档和命名空间管理器之间进行匹配 - 但如果他们这样做,则可能更容易
答案 1 :(得分:3)
您可以使用LINQ-to-XML,如下所示:
var document = XDocument.Load("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=&zipCodeList=&listZipCodeList=¢erPointLat=¢erPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=§or=&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");
var appSampleElements = document.Descendants(XName.Get("Forecast_Gml2Point", "http://www.weather.gov/forecasts/xml/OGC_services")).ToList();
var gmlSampleElements = document.Descendants(XName.Get("Box", "http://www.opengis.net/gml")).ToList();
对于以app
为前缀的用户名,请使用“http://www.weather.gov/forecasts/xml/OGC_services”命名空间。对于那些以gml
为前缀的用户名,请使用“http://www.opengis.net/gml”命名空间。
使用XmlDocument:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("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=&zipCodeList=&listZipCodeList=¢erPointLat=¢erPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=§or=&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");
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("app", "http://www.weather.gov/forecasts/xml/OGC_services");
namespaceManager.AddNamespace("gml", "http://www.opengis.net/gml");
var appNodes = xmlDoc.SelectNodes("//app:Forecast_Gml2Point", namespaceManager);
var gmlNode = xmlDoc.SelectSingleNode("//gml:Box", namespaceManager);