【发布时间】:2011-08-28 18:07:52
【问题描述】:
我有以下 xml 文件
<?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>
我想使用 linq 来提取用户名为“John Smith”的测试的日期和点..
我将如何构建我的 linq?
我做了以下事情,但没有按我的意愿工作:
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());
【问题讨论】:
-
您的代码不应该编译。您正在使用
users,您应该使用user。而且您的t初始化中有错字。