0

I just started using C# ASP.NET MVC. How do I make a login form that used the data from XML file instead of a database then compare the email and password if they are the same. After that then it will show the login page. I was unable to find any example on this program

The XML file

<?xml version="1.0" encoding="utf-8"?>
<Users>
    <User>
        <Name>Test</Name>
        <Password>123</Password>
        <Email>test@hotmail.com</Email>
        <UserID>Admin</UserID>
    </User>
</Users>

The code in the HomeController:

[HttpPost]
public ActionResult Login(Models.Registration obj)
{
    if (ModelState.IsValid)
    {
        IEnumerable<string> email = from users in
                                        XDocument.Load(Server.MapPath("~/XML/User.xml")).Descendants("Users")
                                    select users.Element("Email").Value;

        IEnumerable<string> password = from users in
                        XDocument.Load(Server.MapPath("~/XML/User.xml")).Descendants("Users")
                                       select users.Element("password").Value;

        foreach (var user in email)
        {
             // stuck at this part
        }
    }

    return View();
}

The Login.cshtml:

@{
    ViewBag.Title = "Login";
}

<h2 id="title">@ViewBag.Title</h2>

<div>
    @using (Html.BeginForm("Login", "Home", FormMethod.Post))
    {
        <table>
            <tr>
                <td>@Html.LabelFor(a => a.Email):</td>
                <td>@Html.TextBoxFor(a => a.Email)</td>
                <td>@Html.ValidationMessageFor(a => a.Email)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(a => a.Password):</td>
                <td>@Html.PasswordFor(a => a.Password)</td>
                <td>@Html.ValidationMessageFor(a => a.Password)</td>
            </tr>
            <tr>
                <td colspan="2" align="right">
                    <input type="submit" value="Login"/>
                </td>
            </tr>
        </table>
    }
</div>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0