Linq to XML, select all attributes and their values for a given node

前端 未结 4 1375
时光说笑
时光说笑 2021-01-22 13:06

I have an xml mapping file that looks something like this


    
        

        
4条回答
  •  渐次进展
    2021-01-22 14:02

    Update

    Summarized in a method:

    public IEnumerable GetAttributes(string modelName, string colour)
    {
        XDocument mappings = XDocument.Load(@"D:\colour_mappings.xml");
    
        var q1 =
            from elm in mappings.Descendants("model")
            where (string)elm.Attribute("name") == "modelY"
            select elm;
    
        var q2 =
            from elm in q1.Descendants("mapping")
            where (string)elm.Attribute("colour") == "White"
            select elm.Attributes().Where(a => a.Name != "colour");
    
    
        foreach (IEnumerable attributeList in q2)
        {
            foreach (XAttribute attribute in attributeList)
            {
                yield return attribute;
            }
        }
    }
    

提交回复
热议问题