Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Windows Mobile Password Safe

, 12 Jan 2009
A password safe with a touch screen UI introducing Fluid Controls.
PasswordSafeExe.zip
PasswordSafeSource.zip
PasswordSafe
Fluditity
bin
Classes
vssver2.scc
Controls
enums
vssver2.scc
Events
vssver2.scc
Interfaces
vssver2.scc
vssver2.scc
Fluid.csproj.user
GdiPlus
mssccprj.scc
NativeMethods
vssver2.scc
vssver2.scc
Native
vssver2.scc
obj
Properties
vssver2.scc
vssver2.scc
PasswordSafe
bin
Release
Fluid.dll
passwords.pws
PasswordSafe.exe
Classes
vssver2.scc
Encryption
vssver2.scc
EventArgs
vssver2.scc
ListBoxes
vssver2.scc
PasswordSafe.csproj.user
Properties
vssver2.scc
Resources
lock.bmp
minus.png
props.bmp
save.bmp
saved.bmp
search.bmp
searchd.bmp
undo24.bmp
undo24_d.bmp
Untitled.png
vssver2.scc
security.ico
Templates
vssver2.scc
Themes
vssver2.scc
vssver2.scc
Xml
vssver2.scc
vssver2.scc
using System;

using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using PasswordSafe.Classes;

namespace PasswordSafe.Xml
{
    public class PasswordReader : IDisposable
    {
        public PasswordReader(string xml)
            : base()
        {
            LoadXml(xml);
        }

        private PasswordReader()
            : base()
        {
        
        }

        public static IEnumerable<PasswordCategory> ReadCategories(string xml)
        {
            using (PasswordReader passwordReader = new PasswordReader(xml))
            {
                return passwordReader.Categories;
            }
        }

        private Dictionary<string, PasswordCategory> categories = new Dictionary<string, PasswordCategory>();


        public IEnumerable<PasswordCategory> Categories { get { return categories.Values; } }

        public IEnumerable<PasswordData> Passwords { get { return passwords; } }

        private void LoadXml(string xml)
        {
            passwords.Clear();

            StringReader sr = new StringReader(xml);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            settings.IgnoreWhitespace = true;
            using (XmlReader reader = XmlTextReader.Create(sr, settings))
            {
                if (reader.ReadToFollowing("categories"))
                {
                    ReadCategories(reader);
                }
                if (reader.ReadToFollowing("passwords"))
                {
                    ReadPasswords(reader);
                }
            }
        }

        private PasswordCategory FindCategory(string name)
        {
            if (string.IsNullOrEmpty(name)) return null;
            if (categories.ContainsKey(name)) return categories[name];
            return null;
        }

        private void ReadCategories(XmlReader reader)
        {
            categories.Clear();
            using (XmlReader subTree = reader.ReadSubtree())
            {
                subTree.ReadStartElement();
                while (subTree.Name == "category")
                {
                    PasswordCategory category = new PasswordCategory();
                    string name = subTree["name"];
                    category.Name = name.ToLower();
                    string title = subTree["title"];
                    category.Title = string.IsNullOrEmpty(title) ? name : title;
                    ReadCategoryFields(category, subTree);
                    if (!categories.ContainsKey(category.Name)) categories.Add(category.Name, category);
                    if (!subTree.ReadToNextSibling("category")) break;
                }
            }
        }

        private void ReadCategoryFields(PasswordCategory category, XmlReader reader)
        {
            using (XmlReader subTree = reader.ReadSubtree())
            {
                subTree.ReadStartElement();
                while (subTree.Name == "field" || subTree.Name == "separator")
                {
                    if (subTree.Name == "field")
                    {
                        FieldInfo field = new FieldInfo();
                        string name = subTree["name"];
                        string title = subTree["title"];
                        if (string.IsNullOrEmpty(title)) title = name;
                        field.Name = name.ToLower();
                        field.Title = title;
                        field.Type = GetType(subTree["type"]);
                        category.Fields.Add(field);
                    }
                    else
                    {
                        Separator separator = new Separator();
                        separator.Title = subTree["title"];
                        category.Fields.Add(separator);
                    }
                    subTree.Read();

                }
            }
        }

        private FieldType GetType(string name)
        {
            if (string.IsNullOrEmpty(name)) return FieldType.String;

            try
            {
                return (FieldType)Enum.Parse(typeof(FieldType), name, true);
            }
            catch
            {
                return FieldType.String;
            }
        }

        private void ReadPasswords(XmlReader reader)
        {
            using (XmlReader subTree = reader.ReadSubtree())
            {
                subTree.ReadStartElement();
                while (subTree.Name == "password")
                {
                    PasswordData data = new PasswordData();
                    data.Category = FindCategory(subTree["category"]);
                    passwords.Add(data);

                    if (subTree.MoveToFirstAttribute())
                    {
                        do
                        {
                            string name = subTree.Name;
                            string value = subTree.Value;

                            data.Values.Add(name, value);
                        } while (subTree.MoveToNextAttribute());
                    }
                    if (!subTree.ReadToNextSibling("password")) break;
                }
            }
        }

        private List<PasswordData> passwords = new List<PasswordData>();

        #region obsolete
#if false
        public IEnumerable<PasswordData> GetPasswords()
        {
            string path = @"\My Documents\" + fileName;
            StreamReader sr = File.OpenText(path);
            //using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                settings.IgnoreWhitespace = true;
                using (XmlReader reader = XmlTextReader.Create(sr, settings))
                {
                    if (reader.ReadToFollowing("passwords"))
                    {
                        using (XmlReader subTree = reader.ReadSubtree())
                        {
                            subTree.ReadStartElement();
                            while (subTree.Name == "item")
                            {
                                PasswordData data = new PasswordData();
                                data.Name = subTree["name"];
                                data.EMail = subTree["email"];
                                data.Password = subTree["password"];
                                data.Url = subTree["url"];
                                data.User = subTree["user"];
                                yield return data;
                                if (!subTree.ReadToNextSibling("item")) break;
                            }
                        }
                    }
                }
            }
        }
#endif
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Thomas Gerber
Software Developer (Senior)
Germany Germany
MCPD
Enterprise Application Developer 3.5
Windows Developer 3.5
.ASP.NET Developer 3.5
.NET 2.0 Windows Developer
.NET 2.0 Web Developer
.NET 2.0 Enterprise Application Developer
 

MCTS
.NET 3.5 Windows Forms Applications
.NET 3.5 ASP.NET Applications
.NET 3.5, ADO.NET Application Development
.NET 3.5 WCF
.NET 3.5 WPF
.NET 3.5 WF
Microsoft SQL Server 2008, Database Development
.NET 2.0 Windows Applications
.NET 2.0 Web Applications
.NET 2.0 Distributed Applications
SQL Server 2005
Sharepoint Services 3.0 Application Development
Windows Vista Client Configuration

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 12 Jan 2009
Article Copyright 2009 by Thomas Gerber
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid