Hi all
I have a simple login form with Username and password as fields when the user clicks the login I wish to check the xml whether the username and password is present in the xml. How do I do this, could someone please give me some ideas ? My xml file looks like this

<usersList>
    <user>
        <userid>usr001</userid>
        <username>Administrator</username>
        <password>adminpwd</password>
        <email>admin@admin.com</email>
    </user>
    <user>
        <userid>usr002</userid>
        <username>shopuser</username>
        <password>shopuserpwd</password>
        <email>suser@admin.com</email>
    </user>
    ....
</userList>

So my xml contains many users like the one shown above, but how do I check whether the username entered at the username textbox is present in this xml file ?

Recommended Answers

All 3 Replies

You could use a conditional that tested for empty nodes

foreach (XmlNode node in nodes)
{
// Conditional goes here                    direction=node.ChildNodes[0].ChildNodes[0].Value.ToString();
comment=node.ChildNodes[1].ChildNodes[0].Value.ToString();
image_id= node.Attributes[0].Value.ToString();
}

You will work better with Linq for XML; it is simpler and allows to use IEnumerable Linq extensions.

You can use XMLSerializer. To hold the data from the XML file, I use two classes. I will call them "UserListInfo.cs" and "UserInfo.cs"--you can name them anything you want (it is possible to specify that the element name is different from the class name).

UserInfo.cs:

  • Add using System.Xml.Serialization;

    [XmlRootAttribute(ElementName="user")]
    public class UserInfo
    {
        public string userid { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string email { get; set; }
    
    }//class
    

Note: ElementName="user" specifies that the element name is "user", not "UserInfo". The default is to use the class name unless otherwise specified.

UserListInfo.cs:

  • Add using System.Xml.Serialization;

    [XmlRootAttribute(ElementName = "usersList")]
    public class UserListInfo
    {
        //Element
        [XmlElement()]
        public List<UserInfo> user = new List<UserInfo>();
    
    }//class
    

Note: ElementName="usersList" specifies that the element name is "usersList", not "UserListInfo".

To read the data:

Version 1 (uses BufferedStream):

private void deserializeFromXml(string filename) { //create new instance of UserListInfo UserListInfo myUserListInfo = new UserListInfo(); using (FileStream fs = File.Open(filename,FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
        using (BufferedStream bs = new BufferedStream(fs))
        {
        using (StreamReader sr = new StreamReader(bs))
        { 
        //create new instance of XmlSerializer
        XmlSerializer deserializer = new XmlSerializer(myUserListInfo.GetType());
        myUserListInfo = (UserListInfo)deserializer.Deserialize(sr);
        sr.Close();
        bs.Close();
        fs.Close();
        }//using
        }//using }//using string output = string.Empty; for (int i = 0; i < myUserListInfo.user.Count; i++) {
        output += myUserListInfo.user[i].userid;
        output += " " + myUserListInfo.user[i].password;
        output += System.Environment.NewLine; }//for //print out the data for testing purposes MessageBox.Show(output); }//deserializeFromXml 

Version 2:

private void deserializeFromXml(string filename) { //create new instance of UserListInfo UserListInfo myUserListInfo = new UserListInfo(); using (StreamReader sr = new StreamReader(filename)) {
        //create new instance of XmlSerializer
        XmlSerializer deserializer = new XmlSerializer(myUserListInfo.GetType());
        myUserListInfo = (UserListInfo)deserializer.Deserialize(sr);
        sr.Close(); }//using string output = string.Empty; for (int i = 0; i < myUserListInfo.user.Count; i++) {
        output += myUserListInfo.user[i].userid;
        output += " " + myUserListInfo.user[i].password;
        output += System.Environment.NewLine; }//for //print out the data for testing purposes MessageBox.Show(output); }//deserializeFromXml 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.