Click here to Skip to main content
15,896,726 members
Articles / Programming Languages / XML

A cool utility to convert XML schemas to classes

Rate me:
Please Sign up or sign in to vote.
4.53/5 (7 votes)
9 Oct 2009CPOL4 min read 46.4K   14K   49  
A cool utility to convert XML schemas to classes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.CodeDom;
using System.Xml.Schema;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace XMLtoClass
{
    public partial class codeExplorer : Form
    {
        public codeExplorer(ref CodeNamespace ns)
        {
            InitializeComponent();
            createStructure(ns);

        }

        private void createStructure(CodeNamespace ns)
        {
            treeView1.Nodes.Clear();
            TreeNode n, m;
            n = treeView1.Nodes.Add(ns.Name);
            n.Tag = ns;
            n.ImageIndex = 4;
            foreach (CodeTypeDeclaration c in ns.Types)
            {
                m = n.Nodes.Add(c.Name);
                m.Tag = c;
                m.ImageIndex = 2;
                ParseCodeType(c, m);
            }
        }

        private void ParseCodeType(CodeTypeDeclaration c, TreeNode parent)
        {
            TreeNode n;
            foreach (CodeObject cc in c.Members)
            {
                if (cc is CodeTypeConstructor)
                {
                    n = parent.Nodes.Add(((CodeTypeConstructor)cc).Name);
                    n.Tag = cc;
                    n.ImageIndex = 1;
                }
            }

            foreach (CodeObject cc in c.Members)
            {
                if (cc is CodeMemberMethod)
                {
                    n = parent.Nodes.Add(((CodeMemberMethod)cc).Name);
                    n.Tag = cc;
                    n.ImageIndex = 3;
                }
            }

            foreach (CodeObject cc in c.Members)
            {
                if (cc is CodeMemberField)
                {
                    n = parent.Nodes.Add(((CodeMemberField)cc).Name);
                    n.Tag = cc;
                    n.ImageIndex = 3;
                }
            }
            foreach (CodeObject cc in c.Members)
            {
                if (cc is CodeMemberProperty)
                {
                    n = parent.Nodes.Add(((CodeMemberProperty)cc).Name);
                    n.Tag = cc;
                    n.ImageIndex = 5;
                }
            }

        }

        private void addCoupleToList(string propertyName, string propertyValue)
        {
            ListViewItem l = listView1.Items.Add(propertyName);
            l.SubItems.Add(propertyValue);
        }

        private void addCoupleToList(string propertyName, string propertyValue, string tag)
        {

            ListViewItem l = listView1.Items.Add(propertyName);
            l.SubItems.Add(propertyValue);
            l.Tag = tag;
        }

        private void addCoupleToList(string propertyName, string propertyValue, string tag, string groupKey)
        {

            ListViewItem l = listView1.Items.Add(propertyName);
            l.SubItems.Add(propertyValue);
            l.Tag = tag;
            l.Group = listView1.Groups[groupKey];
        }

        private void renderNameSpace(CodeNamespace ns) {
            addCoupleToList("Object type", "Namespace","","general");
            addCoupleToList("Object name", ((CodeNamespace)ns).Name, "", "general");

            if (((CodeNamespace)ns).Comments.Count > 0)
            {
                addCoupleToList("Has comments", "True", "addComment", "comments");
            }
            else
            {
                addCoupleToList("Has comments", "False", "addComment", "comments");
            }


        }

        private void renderMemberField(CodeMemberField co)
        {
            addCoupleToList("Object type", "Field", "", "general");
            addCoupleToList("Object name", ((CodeMemberField)co).Name, "", "general");
            if (((CodeMemberField)co).CustomAttributes.Count > 0)
            {
                addCoupleToList("Has custom attributes", "True", "", "general");
            }
            else
            {
                addCoupleToList("Has custom attributes", "False", "", "general");
            }

            addCoupleToList("Specified type", ((CodeMemberField)co).Type.BaseType, "", "typeInfo");

            if (((CodeMemberField)co).Comments.Count > 0)
            {
                addCoupleToList("Has comments", "True", "addComment",  "comments");
            }
            else
            {
                addCoupleToList("Has comments", "False", "addComment",  "comments");
            }
        }

        private void renderMemberProperty(CodeMemberProperty co) {
            addCoupleToList("Object type", "Propriet�", "", "general");
            addCoupleToList("Object name", ((CodeMemberProperty)co).Name, "", "general");
            if (((CodeMemberProperty)co).CustomAttributes.Count > 0)
            {
                addCoupleToList("Has custom attributes", "True", "", "general");
            }
            else
            {
                addCoupleToList("Has custom attributes", "False", "", "general");
            }

            addCoupleToList("Specified type", ((CodeMemberProperty)co).Type.BaseType, "", "general");

            if (((CodeMemberProperty)co).Comments.Count > 0)
            {
                addCoupleToList("Has comments", "True", "addComment", "comments");
            }
            else
            {
                addCoupleToList("Has comments", "False", "addComment", "comments");
            }

        }

        private void renderTypeDeclaration(CodeTypeDeclaration co) {
            addCoupleToList("Object type", "Type definition","","general");
            addCoupleToList("Object name", ((CodeTypeDeclaration)co).Name,"","general");

            if (((CodeTypeDeclaration)co).CustomAttributes.Count > 0)
            {
                addCoupleToList("Has custom attributes", "True", "", "general");
            }
            else
            {
                addCoupleToList("Has custom attributes", "False", "", "general");
            }

            addCoupleToList("Class", System.Convert.ToString(((CodeTypeDeclaration)co).IsClass), "", "typeInfo");
            addCoupleToList("Enumeration", System.Convert.ToString(((CodeTypeDeclaration)co).IsEnum), "", "typeInfo");
            addCoupleToList("Interface", System.Convert.ToString(((CodeTypeDeclaration)co).IsInterface), "", "typeInfo");
            addCoupleToList("Struct", System.Convert.ToString(((CodeTypeDeclaration)co).IsStruct), "", "typeInfo");
            addCoupleToList("Modifier -> Partial", System.Convert.ToString(((CodeTypeDeclaration)co).IsPartial), "", "typeInfo");
            addCoupleToList("Base types", System.Convert.ToString(((CodeTypeDeclaration)co).BaseTypes.Count), "", "typeInfo");
            addCoupleToList("Child members", System.Convert.ToString(((CodeTypeDeclaration)co).Members.Count), "", "memberInfo");

            if (((CodeTypeDeclaration)co).Comments.Count > 0)
            {
                addCoupleToList("Has comments", "True", "addComment", "comments");
            }
            else
            {
                addCoupleToList("Has comments", "False", "addComment", "comments");
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            CodeObject co = (CodeObject)e.Node.Tag;

            listView1.Items.Clear();
            listView1.Groups.Add("general", "General info");
            listView1.Groups.Add("typeInfo", "Type info");
            listView1.Groups.Add("memberInfo", "Member info");
            listView1.Groups.Add("comments", "Documentation / Comments");
            if (co is CodeNamespace)
            {
                renderNameSpace((CodeNamespace)co);
}
            if (co is CodeMemberField)
            {
                renderMemberField((CodeMemberField)co);
            }
            if (co is CodeMemberProperty)
            {
                renderMemberProperty((CodeMemberProperty)co);
            }
            if (co is CodeTypeDeclaration)
            {
                renderTypeDeclaration((CodeTypeDeclaration)co);
            }



        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            switch (listView1.SelectedItems[0].Tag.ToString().ToLower())
            {

                case "addcomment":
                    CodeTypeMember mm = (CodeTypeMember)treeView1.SelectedNode.Tag;
                    addCommentWz cm = new addCommentWz(ref mm);

                    Point p = this.Location;
                    p.Y += 30;
                    p.Offset(listView1.Location);
                    p.Offset(listView1.SelectedItems[0].Position);
                    cm.StartPosition = FormStartPosition.Manual;
                    cm.Location = p;
//                    cm.Location = this.PointToClient(listView1.SelectedItems[0].Position);
                    //        cm.Location = new Point(listView1.SelectedItems[0].Position.X + listView1.Location.X, listView1.SelectedItems[0].Position.Y + listView1.Location.Y);
                    cm.Show(this);
                    break;

            }
        }

        
    }
    }


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
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions