首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在XElement上选择具有命名空间别名而不是URI的命名空间XML节点属性

在XElement上选择具有命名空间别名而不是URI的命名空间XML节点属性
EN

Stack Overflow用户
提问于 2012-02-17 21:36:05
回答 2查看 5.9K关注 0票数 4

我试图从一个重命名空间的XML文档中查询一些信息,并且在查找也是名称空间的属性时遇到了一些困难。

XML看起来像:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:skos="http://www.w3.org/2004/02/skos/core#"
    xmlns:geo="http://www.geonames.org/ontology#"
    xmlns:dc="http://purl.org/dc/terms/"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:void="http://rdfs.org/ns/void#">

    <geo:Country rdf:about="http://ontologi.es/place/AD" skos:notation="AD" rdfs:label="Andorra" />
    <geo:Country rdf:about="http://ontologi.es/place/AE" skos:notation="AE" rdfs:label="United Arab Emirates" />
    <geo:Country rdf:about="http://ontologi.es/place/AF" skos:notation="AF" rdfs:label="Afghanistan" />
    <geo:Country rdf:about="http://ontologi.es/place/AG" skos:notation="AG" rdfs:label="Antigua &amp; Barbuda" />
    <geo:Country rdf:about="http://ontologi.es/place/AI" skos:notation="AI" rdfs:label="Anguilla" />
    <geo:Country rdf:about="http://ontologi.es/place/AL" skos:notation="AL" rdfs:label="Albania" /> 
    ...

</rdf:RDF>

我的目标是创建一个具有国家代码和国家名称的对象列表。下面是对我有用的东西:

代码语言:javascript
复制
XmlReader reader = XmlReader.Create(@"path/to/xml.xml");
XDocument root = XDocument.Load(reader);
XmlNameTable nameTable = reader.NameTable;

XmlNamespaceManager nsManager = new XmlNamespaceManager(nameTable);
nsManager.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
nsManager.AddNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
nsManager.AddNamespace("skos", "http://www.w3.org/2004/02/skos/core#");
nsManager.AddNamespace("geo", "http://www.geonames.org/ontology#");

var geoCountries = 
    from country in root.XPathSelectElements("./rdf:RDF/geo:Country", nsManager)
    select new {
        CountryCode = country.Attributes("{http://www.w3.org/2004/02/skos/core#}notation").First().Value,
        CountryName = country.Attributes("{http://www.w3.org/2000/01/rdf-schema#}label").First().Value
    };

这很好,但是我希望使用名称空间别名找到属性,而不是名称空间URI (仅仅是因为),或者至少能够使用别名查找URI。为了尝试后一种想法,我最终发现我可以这样做:

代码语言:javascript
复制
country.Attributes(nsManager.LookupNamespace("skos") + "notation").First().Value

但是我得到了一个XmlException:':‘字符,十六进制值0x3A,不能包含在名称中。

于是我试着:

代码语言:javascript
复制
country.Attributes("{" + nsManager.LookupNamespace("skos") + "}notation").First().Value

然后它就能工作了,但是看起来可能或者应该有一种更简单的方法,或者更确切地说,{namespace}attribute语法在我看来是愚蠢的,就像在框架中抽象的东西一样。

有了所有这些,有什么快捷方式或更简单的方法来查找命名空间的

  • So attributes?

如有任何反馈,我将不胜感激。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-17 21:48:06

使用Linq到xml

代码语言:javascript
复制
XNamespace skos = XNamespace.Get("http://www.w3.org/2004/02/skos/core#");
XNamespace geo = XNamespace.Get("http://www.geonames.org/ontology#");
XNamespace rdfs = XNamespace.Get("http://www.w3.org/2000/01/rdf-schema#");

XDocument rdf = XDocument.Load(new StringReader(xmlstr));
foreach(var country in rdf.Descendants(geo + "Country"))
{
    Console.WriteLine(
        country.Attribute(skos + "notation").Value + " "  + 
        country.Attribute(rdfs + "label").Value );
}
票数 10
EN

Stack Overflow用户

发布于 2019-09-22 16:07:16

您可以使用System.Linq:

代码语言:javascript
复制
country.Attributes().Where(a => a.Name.LocalName == "notation")?.First()?.Value;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9335756

复制
相关文章

相似问题

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