我有一个catalog.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<thredds:catalog xmlns:thredds="http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:bes="http://xml.opendap.org/ns/bes/1.0#">
<thredds:service name="dap" serviceType="OPeNDAP" base="/opendap/hyrax" />
<thredds:service name="file" serviceType="HTTPServer" base="/opendap/hyrax" />
<thredds:dataset name="/allData/51/MOD08_D3/2013/278" ID="/opendap/hyrax/allData/51/MOD08_D3/2013/278/">
<thredds:dataset name="MOD08_D3.A2013278.051.2013298172452.hdf" ID="/opendap/hyrax/allData/51/MOD08_D3/2013/278/MOD08_D3.A2013278.051.2013298172452.hdf">
<thredds:dataSize units="bytes">101753426</thredds:dataSize>
<thredds:date type="modified">2013-10-25T17:38:24</thredds:date>
<thredds:access serviceName="dap" urlPath="/allData/51/MOD08_D3/2013/278/MOD08_D3.A2013278.051.2013298172452.hdf" />
</thredds:dataset>
</thredds:dataset>
</thredds:catalog>我试着把它弄成:
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("d:\\km\\catalog.xml");
XmlNodeList elemList = doc.GetElementsByTagName("dataset name");
for (int i = 0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
} 但是价值观没有被打印出来。我试图像这样解析XML:
var str8 = XElement.Parse("D:\\km\\catalog.xml");
var str1 = XElement.Parse(str8);
var result = str1.Element("thredds:dataset").
Where (x => x.Element("serviceName").Value.Equals("dap")).ToList();
Console.WriteLine(result); 但它没有成功。
我需要从
thredds:dataset name="MOD08_D3.A2013278.051.2013298172452.hdf
有人能给我一个答案吗?
有关更多名称空间,如何分配这些属性名称?示例:
thredds:catalog xmlns:thredds="http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:bes="http://xml.opendap.org/ns/bes/1.0#">
<thredds:service name="dap" serviceType="OPeNDAP" base="/opendap/hyrax"/>
<thredds:service name="file" serviceType="HTTPServer" base="/opendap/hyrax"/>
<thredds:service name="wms" serviceType="WMS" base="/ncWMS/wms"/>
<thredds:dataset name="/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014" ID="/opendap/hyrax/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/">
<thredds:dataset name="OMI-Aura_L3-OMTO3e_2014m0101_v003-2014m0103t013658.he5" ID="/opendap/hyrax/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0101_v003-2014m0103t013658.he5">
<thredds:dataSize units="bytes">3003100</thredds:dataSize>
<thredds:date type="modified">2014-01-03T06:48:48</thredds:date>
<thredds:access serviceName="dap" urlPath="/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0101_v003-2014m0103t013658.he5"/>
<thredds:access serviceName="wms" urlPath="?DATASET=lds/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0101_v003-2014m0103t013658.he5&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities"/>
</thredds:dataset>
<thredds:dataset name="OMI-Aura_L3-OMTO3e_2014m0102_v003-2014m0104t004027.he5" ID="/opendap/hyrax/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0102_v003-2014m0104t004027.he5">
<thredds:dataSize units="bytes">3030178</thredds:dataSize>
<thredds:date type="modified">2014-01-04T05:49:18</thredds:date>
<thredds:access serviceName="dap" urlPath="/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0102_v003-2014m0104t004027.he5"/>
<thredds:access serviceName="wms" urlPath="?DATASET=lds/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0102_v003-2014m0104t004027.he5&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities"/>
</thredds:dataset>
<thredds:dataset name="OMI-Aura_L3-OMTO3e_2014m0103_v003-2014m0105t013415.he5" ID="/opendap/hyrax/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0103_v003-2014m0105t013415.he5">
<thredds:dataSize units="bytes">3019723</thredds:dataSize>
<thredds:date type="modified">2014-01-05T06:49:33</thredds:date>
<thredds:access serviceName="dap" urlPath="/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0103_v003-2014m0105t013415.he5"/>
<thredds:access serviceName="wms" urlPath="?DATASET=lds/HDF-EOS5/Aura_OMI_Level3/OMTO3e.003/2014/OMI-Aura_L3-OMTO3e_2014m0103_v003-2014m0105t013415.he5&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities"/>
</thredds:dataset>发布于 2016-01-19 09:36:34
“限定名”由两个部分组成:本地名称和命名空间。由于名称空间非常冗长,所以通过为名称空间声明前缀来简化。
所以,你要找的是你能看到的是thredds:dataset。这里使用的前缀是thredds,它是名称a http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0的前缀。因此,您需要找到具有该名称的元素:
XNamespace thredds = "http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0";
XName dataset = thredds + "dataset";这样就可以获得第二个dataset元素的name属性:
var doc = XDocument.Load(@"path\to\file.xml");
var name = doc.Descendants(dataset)
.Skip(1)
.Select(x => (string)x.Attribute("name"))
.Single();有关工作示例,请参见这把小提琴。
https://stackoverflow.com/questions/34872954
复制相似问题