Click here to Skip to main content
15,886,422 members
Articles / Web Development / ASP.NET

Web Profile Class Generator

Rate me:
Please Sign up or sign in to vote.
3.86/5 (8 votes)
30 Jul 2008CPOL2 min read 34.4K   482   22  
This program will generate a class with easy access to custom properties used in the ASP.NET membership feature.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.Configuration;
using System.Collections.Specialized;
using System.Web.Configuration;

namespace WebProfileGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region Control events 
        private void btnFileDialog_Click(object sender, EventArgs e)
        {
            this.openFileDialog.ShowDialog();
        }

        private void openFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            this.txtPath.Text = this.openFileDialog.FileName;
        }

        private void txtCode_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.txtCode.SelectAll();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            GenerateCode();
        }
        #endregion

        private void GenerateCode()
        {
            // Clears the code TextBox
            this.txtCode.Text = "";

            // Check if file exists
            if (!System.IO.File.Exists(this.txtPath.Text))
            {
                MessageBox.Show("Could not load selected file\nTry select another file!", "File error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Load selected web.config into a XmlDocument
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(this.txtPath.Text);

            // Extract nodelist for properties and groups
            XmlNodeList propertyNodes = xmlDoc.SelectNodes("/configuration/system.web/profile/properties/add");
            XmlNodeList groupNodes = xmlDoc.SelectNodes("/configuration/system.web/profile/properties/group");

            // Check if there are any properties/groups
            if ((propertyNodes.Count + groupNodes.Count) == 0)
            {
                MessageBox.Show("Could not find any profile-section in selected web.cinfg file\nTry select another file!", "No profile found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // Write class
            AddCodeLine("public static class " + this.txtClassName.Text);
            AddCodeLine("{");

            // Loop through all groups
            foreach (XmlNode node in groupNodes)
            {
                AddCodeLine("public class " + node.Attributes["name"].Value + "{");
                // Loop through properties in each group
                foreach (XmlNode subNode in node.SelectNodes("add"))
                {
                    AddCodeLine(new ProfileProperty(subNode).ToCode());
                }
                AddCodeLine("}");
            }

            // Loop through properties in each group
            foreach (XmlNode node in propertyNodes)
            {
                AddCodeLine(new ProfileProperty(node).ToCode());
            }

            // Add Save-method
            AddCodeLine("public static void Save()");
            AddCodeLine("{");
            AddCodeLine("HttpContext.Current.Profile.Save();");
            AddCodeLine("}");

            // End class
            AddCodeLine("}");
        }

        /// <summary>
        /// Adds more text (code) to the txtCode TextBox
        /// </summary>
        /// <param name="code"></param>
        private void AddCodeLine(string code)
        {
            this.txtCode.Text = this.txtCode.Text + "\r\n" +code;
        }

        /// <summary>
        /// Opens a browser with my homepage :-)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string uri = @"http://www.serverside.no/";
            System.Diagnostics.Process browser = new System.Diagnostics.Process();
            browser.StartInfo.FileName = "iexplore.exe";
            browser.StartInfo.Arguments = uri;
            browser.Start();
        }
    }
}

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)


Written By
Software Developer (Senior) Aurum AS
Norway Norway
Microsoft Certified Solutions Developer (MCSD)

Personal website:
stian.net

My projects:
CRM1.no - A free to use norwegian crm software
Fakturax - A free to use norwegian invoice software
Timeføring.no - A free to use norwegian timereg software
MittUtlegg - A free to use norwegian software for receipts
SupportWeb - A free to use norwegian software customersupport

Comments and Discussions