C# LINQ to XML 返回结果
我正在尝试为 WPF 应用程序创建一个简单的登录页面。我正在使用以下 XML 文件:
<用户>
<用户>
<用户名>test1
<密码>1test
我是 LINQ 新手,不太了解如何获取任何内容或将结果分配给变量。
现在我有:
XDocument users = new XDocument("users.xml");
var queryResults =
from u in users.Root.Descendants("user")
where u.Element("username").Value == tbUserName.Text && u.Element("password").Value == pbPassword.Password
select u;
foreach (var item in queryResult)
{
Console.WriteLine(item);
}
因此,如果我的查询正确,如果 xml 文件中存在登录值,它会将用户名和密码写入控制台。我的理解就这么多了。我不知道接下来要做什么来验证或验证用户登录。任何帮助或想法都会很棒。
I am trying to create a simple login in page for an WPF app. I am using the following XML file:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<username>test1</username>
<password>1test</password>
</user>
</users>
I am new to LINQ and don't really understand how to get anything out or assign the results to a variable.
Right now I have:
XDocument users = new XDocument("users.xml");
var queryResults =
from u in users.Root.Descendants("user")
where u.Element("username").Value == tbUserName.Text && u.Element("password").Value == pbPassword.Password
select u;
foreach (var item in queryResult)
{
Console.WriteLine(item);
}
So if my query is right it will write the username and password to the console, if the login values exist in the xml file. This is as much as I understand. I don't know what to do next to verify or validate the user login. Any help or ideas would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
queryResults
不为空,那么用户名和密码是正确的,所以你可以这样做:If
queryResults
is not empty, then the username and password are correct, so you can do it like this: