C# LINQ to XML 返回结果

发布于 2024-11-03 08:32:13 字数 878 浏览 3 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2024-11-10 08:32:13

如果queryResults不为空,那么用户名和密码是正确的,所以你可以这样做:

if (queryResults.Any())
{
    // log user in, let him continue
}
else
{
    // user name or password is incorrect, let the user know and possibly try again
}

If queryResults is not empty, then the username and password are correct, so you can do it like this:

if (queryResults.Any())
{
    // log user in, let him continue
}
else
{
    // user name or password is incorrect, let the user know and possibly try again
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文