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

前端 未结 2 2001
一整个雨季
一整个雨季 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 );
    }
    
    0 讨论(0)
  • 2021-01-21 12:44

    You can use System.Linq:

    country.Attributes().Where(a => a.Name.LocalName == "notation")?.First()?.Value;
    
    0 讨论(0)
提交回复
热议问题