Introduction
I have looked for a simple way to save user data to an XML file. All the solutions I have found used XML serialization (=> not so simple). So I have decided to create my own application. The data in the XML file must be encoded for security reason.
The following subjects are discussed in this article:
- Encode string using MD5 encoding.
- Load all user database from the XML file to the application.
- Save all user database to an XML file.
- Look for an object in an
ArrayList
.
Background
To change the Desktop background, the application needs to use the WinAPI SystemParametersInfo
and supply it with the desired image parameters. The only problem is that the API accepts only BMP image type. My solution was to create a BMP from each JPG or JPEG file in the directory.
Using the Code
- Encode string using MD5 encoding:
In order to do the encoding, this application creates a MD5 Encoding object. Using StringBuilder
, the string
is then converted into an array of bytes and converted back to string
after the MD5 encoding.
MD5 md5serv = MD5CryptoServiceProvider.Create();
byte[] hash;
StringBuilder stringbuff = new StringBuilder();
ASCIIEncoding asciienc = new ASCIIEncoding();
byte[] buffer = asciienc.GetBytes(raw);
hash = md5serv.ComputeHash(buffer);
foreach (byte b in hash) { stringbuff.Append(b.ToString("x2")); }
return stringbuff.ToString();
- Load all user database from the XML file to the application:
The method used reads the XML file into a XmlTextReader
. Then it searches for new XML elements. For each element, the method extracts its attributes. Attributes are extracted from reader.Value
.
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
elementName = reader.Name;
while (reader.MoveToNextAttribute()) {
if (reader.Name == "Password")
myUser.Password = reader.Value;
}
}
}
- Save all user database to an XML file:
Save all user database to an XML file from the application. The method used here uses the XmlTextWriter
. It saves all the users to a new XML file. First of all, the XML options must be set - like formatting and indentation. Then it writes the start element. After that, for each user, the method adds its attributes (XML attributes) to the user name (XML element).
XmlTextWriter writer = new XmlTextWriter(strPath + "\\users.xml", null);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.WriteStartElement("Users");
foreach (User myUser in myAL) {
for each user (myUser.UserName);
writer.WriteAttributeString("Password", myUser.Password);
writer.WriteAttributeString("Previliage", myUser.Previliage);
writer.WriteEndElement();
}
writer.Flush();
writer.Close();
- Remove an object from an
ArrayList
:
If you want to delete an object from an ArrayList
, you don't need any index. All you need is to find the correct pointer and delete it.
foreach (User myUser in myAL) {
if (myUser.UserName == (string)listBoxUser.SelectedItem)
myAL.Remove(myUser);
}
}
History
- 24th June, 2005: Initial version
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.