首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C#查找XML文件的元素及其属性

如何使用C#查找XML文件的元素及其属性
EN

Stack Overflow用户
提问于 2016-01-19 09:26:22
回答 1查看 101关注 0票数 1

我有一个catalog.xml文件如下:

代码语言:javascript
复制
<?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>

我试着把它弄成:

代码语言:javascript
复制
//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:

代码语言:javascript
复制
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

有人能给我一个答案吗?

有关更多名称空间,如何分配这些属性名称?示例:

代码语言:javascript
复制
    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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-19 09:36:34

“限定名”由两个部分组成:本地名称和命名空间。由于名称空间非常冗长,所以通过为名称空间声明前缀来简化。

所以,你要找的是你能看到的是thredds:dataset。这里使用的前缀是thredds,它是名称a http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0的前缀。因此,您需要找到具有该名称的元素:

代码语言:javascript
复制
XNamespace thredds = "http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0";
XName dataset = thredds + "dataset";

这样就可以获得第二个dataset元素的name属性:

代码语言:javascript
复制
var doc = XDocument.Load(@"path\to\file.xml");

var name = doc.Descendants(dataset)
    .Skip(1)
    .Select(x => (string)x.Attribute("name"))
    .Single();

有关工作示例,请参见这把小提琴

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34872954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档