如何使用C#

时间:2016-01-19 09:26:23

标签: c# xml

我有一个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>

1 个答案:

答案 0 :(得分:1)

一个合格的名字&#39;由两部分组成:本地名称和命名空间。由于命名空间非常冗长,因此通过声明命名空间的前缀来简化这一过程。

因此,您正在寻找可以看到thredds:dataset的内容。这里使用的前缀是thredds,它是namesapce 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();

有关工作示例,请参阅this fiddle