开发者

C# LINQ to XML returned results

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<开发者_JAVA技巧;/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.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜