Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XElement上选择具有命名空间别名而不是URI的命名空间XML节点属性_C#_.net_Xml_Xpath_Xml Namespaces - Fatal编程技术网

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

C# 在XElement上选择具有命名空间别名而不是URI的命名空间XML节点属性,c#,.net,xml,xpath,xml-namespaces,C#,.net,Xml,Xpath,Xml Namespaces,我试图从一个名称空间很大的XML文档中查询出一些信息,但在查找同样具有名称空间的属性时遇到了一些问题 XML看起来像: <?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="ht

我试图从一个名称空间很大的XML文档中查询出一些信息,但在查找同样具有名称空间的属性时遇到了一些问题

XML看起来像:

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

country.Attributes(nsManager.LookupNamespace("skos") + "notation").First().Value
但是我得到了一个
XmlException
:不能在名称中包含“:”字符,十六进制值0x3A

于是我试着:

country.Attributes("{" + nsManager.LookupNamespace("skos") + "}notation").First().Value
然后它就可以工作了,但似乎有一种更简单的方法,或者更确切地说,
{namespace}属性
语法在我看来很愚蠢,就像在框架中被抽象出来的东西一样

  • 那么,有没有快捷方式或更简单的方法来查找名称空间属性?
如果有任何反馈,我将不胜感激。谢谢

使用Linq转换xml

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 );
}

您可以使用System.Linq:

country.Attributes().Where(a => a.Name.LocalName == "notation")?.First()?.Value;
country.Attributes().Where(a => a.Name.LocalName == "notation")?.First()?.Value;