使用linq从xml获取键值对

如何使用linq从此xml示例中提取键值对:

     

试试这个:

 string text = "..."; var pairs = XDocument.Parse(text) .Descendants("add") .Select(x => new { Key = x.Attribute("key").Value, Value = x.Attribute("Value").Value }) .ToList(); 
 XDocument fooXML = new XDocument.Load("foo.xml") var query = from a in fooXML.Element("foo").Elements("add") select new { key = a.Attribute("key").Value, val = a.Attribute("Value").Value }; // Then do what you want with the query...