14

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.

Edit:

        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();

Here the strResult contains XML Data

1
  • 2
    Can you show what have you tried so far, what XML you are trying to parse and what doesn't work. Commented Jun 8, 2011 at 8:02

2 Answers 2

18

Something like:

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.

Sign up to request clarification or add additional context in comments.

Comments

3

You could use LINQ-to-XML, like this:

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=&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");
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();

Use "http://www.weather.gov/forecasts/xml/OGC_services" namespace for those that prefixed with app. Use "http://www.opengis.net/gml" namespace for those that prefixed with gml.

With 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=&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");
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.