Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<environments>
<environment>
<add key="Test1">
<add key="Test2">
<add key="Test3">




how to read all keys using LINQ in C#

What I have tried:

C#
XElement root = new XElement("Root",  
                    new XAttribute("Attr", "abcde")  
                );  
Console.WriteLine(root);  
string str = (string)root.Attribute("Attr");  
Console.WriteLine(str);
Posted
Updated 16-Jul-19 3:31am
v2

1 solution

 
Share this answer
 
Comments
Amit Patel1985 16-Jul-19 10:22am    
Hello Richard,

tried this I am not able to solve using above xml example.
tried below code

IEnumerable<string> partNos = from item in configuratoinFile.Descendants("EnvironmentDetails")
select (string)item.Attribute("add");

Thanks,
amit
Richard Deeming 16-Jul-19 13:54pm    
Based on your sample XML (which isn't valid XML, so I'm guessing here), add is an element, not an attribute.

Try:
from item in configurationFile.Descendants("environment").Descendants("add")
select (string)item.Attribute("key");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900