Click here to Skip to main content
15,881,455 members
Articles / Programming Languages / C#

XGenPlus - A Flexible Tool to Generate Typed XML Serializers for your .NET Applications

,
Rate me:
Please Sign up or sign in to vote.
4.70/5 (10 votes)
12 Nov 2007CPOL7 min read 63.3K   698   36  
XGenPlus is a flexible tool to generate typed XML serializers for your .NET applications. It provides more flexibility than the sgen.exe tool combining the efficiency offered by Mvp.Xml.Xgen library.
//===============================================================================
// XGenPlus Tool
// ==============================================================================
// Please visit http://www.codeplex.com/xgenplus for latest updates.
//
// This source is subject to the GNU General Public License v2.
// 
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections;
using System.CodeDom.Compiler;
using System.Xml;
using System.Xml.Serialization;
using Config;

namespace XGenPlus.Config
{
    class Util
    {
        /// <summary>
        /// Writes the configuration to a config file
        /// </summary>
        /// <param name="p">The parameters to write</param>
        /// <param name="file">The file to write</param>
        public static void PutConfig(Parameters p, string file)
        {
             XGenPlusConfig conf = new  XGenPlusConfig();
             GenerateFor gen = new  GenerateFor();

            gen.Assembly = p.AssemblyName;
            gen.CopyTo = p.CopyTo;
            gen.Nocompile = p.NoCompile;
            gen.Nogenerate = p.NoGenerate;

            if (null != p.ReferenceList)
                for (int i = 0; i < p.ReferenceList.Count; i++)
                {
                     Add a = new  Add();
                    a.Command =  AddCommand.Reference;
                    a.Value = p.ReferenceList[i];
                    gen.AddCollection.Add(a);
                }

            if (null != p.IncludeList)
                for (int i = 0; i < p.IncludeList.Count; i++)
                {
                     Add a = new  Add();
                    a.Command =  AddCommand.Include;
                    a.Value = p.IncludeList[i];
                    gen.AddCollection.Add(a);
                }


            if (null != p.ExcludeList)
                for (int i = 0; i < p.ExcludeList.Count; i++)
                {
                     Add a = new  Add();
                    a.Command =  AddCommand.Exclude;
                    a.Value = p.ExcludeList[i];
                    gen.AddCollection.Add(a);
                }

            conf.Add(gen);

            using (StreamWriter writer = new StreamWriter(file))
            {
                XmlSerializer ser = new XmlSerializer(typeof( XGenPlusConfig));
                ser.Serialize(writer, conf);
            }



        }

        /// <summary>
        /// Reads the configuration from a XGenPlus configuration file
        /// </summary>
        /// <param name="file">The configuration file path</param>
        /// <returns></returns>
        public static Parameters GetConfig(string file)
        {
            Parameters p = new Parameters();
             XGenPlusConfig conf = new  XGenPlusConfig();

            XmlSerializer ser = new XmlSerializer(typeof( XGenPlusConfig));
            conf = ( XGenPlusConfig)ser.Deserialize(new StreamReader(file));

             GenerateFor gen = conf.GenerateForCollection[0];

            p.AssemblyName = gen.Assembly;
            p.CopyTo = gen.CopyTo;
            p.NoCompile = gen.Nocompile;
            p.NoGenerate = gen.Nogenerate;

            if (null != gen.AddCollection)
            {
                foreach ( Add a in gen.AddCollection)
                {
                    if (a.Command ==  AddCommand.Exclude)
                        p.ExcludeList.Add(a.Value);
                    if (a.Command ==  AddCommand.Include)
                        p.IncludeList.Add(a.Value);
                    if (a.Command ==  AddCommand.Reference)
                        p.ReferenceList.Add(a.Value);
                }
            }

            return p;

        }
    }
}

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
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions