Reputation: 1034
I need to read xml documents like this :
<wcs:CoverageOffering>
<wcs:description>Generated from GeoTIFF</wcs:description>
<wcs:name>ndh:ndh-cyclone-mortality-risks-distribution</wcs:name>
....
But in some servers the xml document is implemented without namespace tag :
<CoverageOffering>
<description>Generated from GeoTIFF</description>
<name>ndh:ndh-cyclone-mortality-risks-distribution</name>
....
How can I read both in an efficient way? (I wrote if else statements for each node to control this condition but it seems not a good way to do it)
Upvotes: 1
Views: 128
Reputation: 10357
Use XmlDocument
and add wcs
namespace to XmlNamespaceManager
:
var document = new XmlDocument();
document.Load(...);
var nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("wcs", "http://...your ns");
var nl = document.SelectNodes("your xpath", nsmgr);
Upvotes: 1
Reputation: 11955
You can use this XML Library. It will use a namespace if needed.
You can use it like:
XElement root = XElement.Load(file);
string description = root.Get("path/to/description", default(string));
or
string description = root.XGetElement("//description", default(string));
The default(string)
is for type conversion for either Get. You can pass any default you desire.
Upvotes: 0