Re: Stuck on problem using Serialization

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: David Elliott (Webbert_at_newsgroups.nospam)
Date: 06/10/04


Date: Thu, 10 Jun 2004 17:18:41 -0400

You can drop this code into 1 class file for a console and compile. It will dump
out the file to <project>\bin\debug\output.xml

==================================================================

using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Xml.Serialization;

namespace XmlFile_5
{
        public class MyMain
        {
                [STAThread]
                static void Main(string[] args)
                {
                        DatabaseTable table = null;
                        DatabaseMapping db = new DatabaseMapping("MyImportantTable");

                        // Table 0
                        table = db.AddTable("Table", "New Table Name 0");
                        table.AddColumn("Col_1", "New_Col_1");
                        table.AddColumn("Col_2", "New_Col_2");
                        table.AddColumn("Col_3", "New_Col_3");
                        table.AddColumn("Col_4", "New_Col_4");
                        table.AddColumn("Col_5", "New_Col_5");
                        MyMain.Save(db, "output.xml");
                        db = null;
                }

                static public void Save(DatabaseMapping db, string fileName)
                {
                        TextWriter txtWriter = null;
                        XmlSerializer xmlSerializer = null;
                        Type objType = null;

                        try
                        {
                                objType = db.GetType();
                                xmlSerializer = new XmlSerializer(objType);
                                txtWriter = new StreamWriter(fileName);

                                xmlSerializer.Serialize(txtWriter, db);
                        }
                        catch(Exception ex)
                        {
                                Trace.WriteLine(ex.Message);
                        }
                        finally
                        {
                                // Close The Reader
                                if (txtWriter != null)
                                        txtWriter.Close();
                        }
                }
        }

        // ////////////////////////////////////////////////////////
        // ////////////////////////////////////////////////////////
        [XmlInclude(typeof(DatabaseTable))]
        public class DatabaseMapping
        {
                private string name;
                private ArrayList dbTable = new ArrayList();
                
                public DatabaseMapping()
                {
                }

                public DatabaseMapping(string dbObjectName)
                {
                        name = dbObjectName;
                }

                public string DbObjectName
                {
                        get { return (name); }
                }

                [XmlArray(ElementName = "Tables")]
                [XmlArrayItem("Table", Type = typeof(DatabaseTable))]
                public ArrayList Tables
                {
                        get {return (dbTable); }
                }

                public DatabaseTable AddTable(string nameOfDbTable, string newTableName)
                {
                        DatabaseTable table;

                        table = FindTable(nameOfDbTable);
                        if (table == null)
                        {
                                table = new DatabaseTable(nameOfDbTable, newTableName);
                                dbTable.Add(table);
                        }

                        return (table);
                }

                public DatabaseTable FindTable(string nameOfDbTable)
                {
                        string tempName = nameOfDbTable.Trim().ToLower();

                        foreach (DatabaseTable table in dbTable)
                        {
                                if (table.NameOfTable.ToLower().Equals(tempName))
                                        return (table);
                        }

                        return (null);
                }
        }

        // ////////////////////////////////////////////////////////
        // ////////////////////////////////////////////////////////
        public class DatabaseTable
        {
                private TableName tableName;
                private string name;
                private string className;
                private ArrayList columns = new ArrayList();

                public DatabaseTable(string nameOfDbTable, string newTableName)
                {
                        name = nameOfDbTable.Trim();
                        className = newTableName.Trim();

                        tableName = new TableName(nameOfDbTable, newTableName);
                }

                public DatabaseTable()
                {
                }

                /// <summary>
                /// This will be removed when the next property works
                /// </summary>
                public string NameOfTable
                {
                        get { return (name); }
                        set { name = value.Trim(); }
                }

                /// <summary>
                /// This will replace the previous property
                /// </summary>
                [XmlElement(ElementName = "Dave", Type = typeof(TableName))]
                public TableName MyTableName
                {
                        get {return (tableName); }
                }

                /// <summary>
                /// This was for testing will ultimately be removed
                /// </summary>
                [XmlIgnore]
                public string NewTableName
                {
                        get { return (className); }
                        set { className = value.Trim(); }
                }

                [XmlArray(ElementName = "Columns")]
                [XmlArrayItem(ElementName = "Column", Type = typeof(DatabaseTableColumn))]
                public ArrayList Columns
                {
                        get {return (columns); }
                }

                public DatabaseTableColumn AddColumn(string nameOfDbColumn, string nameOfClassColumn)
                {
                        DatabaseTableColumn col;

                        col = FindColumn(nameOfDbColumn);
                        if (col == null)
                        {
                                col = new DatabaseTableColumn(nameOfDbColumn, nameOfClassColumn);
                                columns.Add(col);
                        }

                        return (col);
                }

                public DatabaseTableColumn FindColumn(string nameOfDbColumn)
                {
                        string tempName = nameOfDbColumn.Trim().ToLower();

                        foreach (DatabaseTableColumn col in columns)
                        {
                                if (col.Name.ToLower().Equals(tempName))
                                        return (col);
                        }

                        return (null);
                }
        }

        // ////////////////////////////////////////////////////////
        // ////////////////////////////////////////////////////////
        public class DatabaseTableColumn
        {
                private string dbColumn;
                private string classColumnName;

                public DatabaseTableColumn()
                {
                }

                public DatabaseTableColumn(string nameOfDbColumn, string nameOfClassColumn)
                {

                        dbColumn = nameOfDbColumn.Trim();
                        classColumnName = nameOfClassColumn.Trim();
                }

                [XmlAttribute]
                public string Name
                {
                        get { return (dbColumn); }
                        set { dbColumn = value.Trim(); }
                }

                [XmlText(Type=typeof(string))]
                public string NewColumnName
                {
                        get { return (classColumnName); }
                        set { classColumnName = value.Trim(); }
                }
        }

        // ////////////////////////////////////////////////////////
        // ////////////////////////////////////////////////////////
        public class TableName
        {
                private string dbName;
                private string newName;

                public TableName()
                {
                }

                /// <summary>
                ///
                /// </summary>
                /// <param name="nameOfDbColumn"></param>
                /// <param name="nameOfClassColumn"></param>
                public TableName(string nameOfDbTable, string newTableName)
                {

                        dbName = nameOfDbTable.Trim();
                        newName = newTableName.Trim();
                }

                [XmlAttribute]
                public string Name
                {
                        get { return (newName); }
                        set { newName = value.Trim(); }
                }

                [XmlText(Type=typeof(string))]
                public string NameOfDbColumn
                {
                        get { return (dbName); }
                        set { dbName = value.Trim(); }
                }
        }
}



Relevant Pages

  • JAVA applet probelems
    ... I have a old JDK installed 1.1.8 purposly to compile a applet below: ... where I get the following output from the java console: ... s: dump system properties ...
    (comp.lang.java.programmer)
  • Re: How do I import a user defined class into VisualStudio .net J#
    ... > I'm using Visual Studio .NET with J#. ... > windowyou get a class file. ... > One example if you have a java file called Test.java and you compile this ...
    (microsoft.public.dotnet.vjsharp)
  • Re: Alternative to JVM
    ... compile class file, but can run Java program with help of a different ... language grammer tree to a 2-address / 3-address instruction set. ... class file from Parse Tree directly. ...
    (comp.lang.java.machine)
  • Re: dependency-detection in java - Take 2
    ... expensive than a full compile? ... determine "relevant" changes between each new .class file and it's ... method-implementations or private members were changed. ...
    (comp.lang.java.programmer)
  • Re: Reuse code on multiple pages of project
    ... > If you use Visual Studio it will do the compile for you and place it in the bin ... > directory AFAIK. ... > drop the class file in the App_Code folder and after importing the namespace you are ...
    (microsoft.public.dotnet.framework.aspnet)