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

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

I have an xml mapping file that looks something like this


    
        

        
4条回答
  •  走了就别回头了
    2021-01-22 13:48

    Just because I like a challenge, here it is in one query:

    XDocument test = XDocument.Parse("                                                                                                                                                                ");
    
    var maps = from model in test.Root.Elements("model")
               from attr in model.Attributes("name")
               from mapping in model.Elements("mapping")
               where attr.Value == "modelY" && mapping.Attribute("colour").Value == "White"
               select new
               {
                     configCode = mapping.Attribute("configCode").Value
                   , stConfigCode = mapping.Attribute("stConfigCode").Value
                   , dgConfigCode = mapping.Attribute("dgConfigCode").Value
               };
    
    foreach (var map in maps)
    {
        Console.Write("configCode: ");
        Console.WriteLine(map.configCode);
        Console.Write("stConfigCode: ");
        Console.WriteLine(map.stConfigCode);
        Console.Write("dgConfigCode: ");
        Console.WriteLine(map.dgConfigCode);
    }
    

提交回复
热议问题