Selecting namespaced XML node attributes with namespace alias instead of URI on an XElement

前端 未结 2 2003
一整个雨季
一整个雨季 2021-01-21 12:16

I\'m trying to query out some information from a heavily namespaced XML document and am having some trouble finding attributes that are also namespaced.

The XML looks li

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 12:35

    using Linq to 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 );
    }
    

提交回复
热议问题