i have the following xml file
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User>
<Name>John Smith</Name>
<test>
<Date>23.05.2011</Date>
<points>33</points>
</test>
<test>
<Date>22.06.2011</Date>
<points>29</points>
</test>
</User>
</Users>
and i would like to use linq to extract the dates and the points of the tests where username is “John Smith”..
how would i build my linq ?
i have done the following, but is not working as i wish :
XElement main = XElement.Load(@"users.xml");
string t = "John Smith";
var v = from user in main.Elements("User")
where t == users.Element("Name").Value
select users;
MessageBox.Show(v.First().Element("Date").Value.ToString());
I’m not sure what format you want the output to be, but this samples code should get the date and points. This projects the results into an anonymous type:
And the output is: