Read command line input and do the xml xsl translation

   

using System;
using System.Xml.Xsl;

public class Transform {
  public static void Main(string [] args) {
    string source = args[0];
    string stylesheet = args[1];
    string destination = args[2];

    XslTransform transform = new XslTransform();
    transform.Load(stylesheet);
    // for .NET v 1.0
    //transform.Transform(source, destination);
    // for .NET v 1.1
    transform.Transform(source, destination, null);
  }
}


           
          


Perform an XSL Transform

   


using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Xsl;


public class TransformXml {
    private static void Main() {
        XslTransform transform = new XslTransform();
            
        // Load the XSL stylesheet.
        transform.Load("orders.xslt");
            
        // Transform orders.xml into orders.html using orders.xslt.
        transform.Transform("orders.xml", "orders.html", null);
    }
}


           
          


Deserializes/Serializes an xml document back into an object

   
 
//New BSD License (BSD)
//http://twitterxml.codeplex.com/license
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace TwitterXml.Utilities
{
    public class XmlSerializerHelper
    {

        /// <summary>
        /// Deserializes an xml document back into an object
        /// </summary>
        /// <param name="xml">The xml data to deserialize</param>
        /// <param name="type">The type of the object being deserialized</param>
        /// <returns>A deserialized object</returns>
        public static object Deserialize(XmlDocument xml, Type type)
        {
            XmlSerializer s = new XmlSerializer(type);
            string xmlString = xml.OuterXml.ToString();
            byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);
            MemoryStream ms = new MemoryStream(buffer);
            XmlReader reader = new XmlTextReader(ms);
            Exception caught = null;

            try
            {
                object o = s.Deserialize(reader);
                return o;
            }

            catch (Exception e)
            {
                caught = e;
            }
            finally
            {
                reader.Close();

                if (caught != null)
                    throw caught;
            }
            return null;
        }

        /// <summary>
        /// Serializes an object into an Xml Document
        /// </summary>
        /// <param name="o">The object to serialize</param>
        /// <returns>An Xml Document consisting of said object&#039;s data</returns>
        public static XmlDocument Serialize(object o)
        {
            XmlSerializer s = new XmlSerializer(o.GetType());

            MemoryStream ms = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(ms, new UTF8Encoding());
            writer.Formatting = Formatting.Indented;
            writer.IndentChar = &#039; &#039;;
            writer.Indentation = 5;
            Exception caught = null;

            try
            {
                s.Serialize(writer, o);
                XmlDocument xml = new XmlDocument();
                string xmlString = ASCIIEncoding.UTF8.GetString(ms.ToArray());
                xml.LoadXml(xmlString);
                return xml;
            }
            catch (Exception e)
            {
                caught = e;
            }
            finally
            {
                writer.Close();
                ms.Close();

                if (caught != null)
                    throw caught;
            }
            return null;
        }

    }
}

   
     


Converts an XML string to an object

   
 
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Xml.Serialization;


namespace Utilities
{
    /// <summary>
    /// Utility class for managing files
    /// </summary>
    public static class FileManager
    {

        /// <summary>
        /// Gets a files&#039; contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <returns>a string containing the file&#039;s contents</returns>
        public static string GetFileContents(string FileName)
        {
            try
            {
                return GetFileContents(FileName, 5000);
            }
            catch { throw; }
        }

        /// <summary>
        /// Gets a files&#039; contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <param name="TimeOut">Amount of time in ms to wait for the file</param>
        /// <returns>a string containing the file&#039;s contents</returns>
        public static string GetFileContents(string FileName, int TimeOut)
        {
            StreamReader Reader = null;
            int StartTime = System.Environment.TickCount;
            try
            {
                bool Opened = false;
                while (!Opened)
                {
                    try
                    {
                        if (System.Environment.TickCount - StartTime >= TimeOut)
                            throw new System.IO.IOException("File opening timed out");
                        Reader = File.OpenText(FileName);
                        Opened = true;
                    }
                    catch (System.IO.IOException e)
                    {
                        throw e;
                    }
                }
                string Contents = Reader.ReadToEnd();
                Reader.Close();
                return Contents;
            }
            catch
            {
                return "";
            }
            finally
            {
                if (Reader != null)
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
        }

    }
    /// <summary>
    /// Helps with serializing an object to XML and back again.
    /// </summary>
    public static class Serialization
    {


        /// <summary>
        /// Takes an XML file and exports the Object it holds
        /// </summary>
        /// <param name="FileName">File name to use</param>
        /// <param name="Object">Object to export to</param>
        /// <param name="ObjectType">Object type to export</param>
        public static void XMLToObject(string FileName, out object Object,Type ObjectType)
        {
            if (string.IsNullOrEmpty(FileName))
            {
                throw new ArgumentException("File name can not be null/empty");
            }
            try
            {
                string FileContent = FileManager.GetFileContents(FileName);
                Object = XMLToObject(FileContent,ObjectType);
            }
            catch { throw; }
        }

        /// <summary>
        /// Converts an XML string to an object
        /// </summary>
        /// <param name="XML">XML string</param>
        /// <param name="ObjectType">Object type to export</param>
        /// <returns>The object of the specified type</returns>
        public static object XMLToObject(string XML,Type ObjectType)
        {
            if (string.IsNullOrEmpty(XML))
            {
                throw new ArgumentException("XML can not be null/empty");
            }
            try
            {
                using (MemoryStream Stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(XML)))
                {
                    XmlSerializer Serializer = new XmlSerializer(ObjectType);
                    return Serializer.Deserialize(Stream);
                }
            }
            catch { throw; }
        }

    }
}

   
     


Takes an XML file and exports the Object it holds

   
 
 
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Xml.Serialization;


namespace Utilities
{
    /// <summary>
    /// Utility class for managing files
    /// </summary>
    public static class FileManager
    {

        /// <summary>
        /// Gets a files&#039; contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <returns>a string containing the file&#039;s contents</returns>
        public static string GetFileContents(string FileName)
        {
            try
            {
                return GetFileContents(FileName, 5000);
            }
            catch { throw; }
        }

        /// <summary>
        /// Gets a files&#039; contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <param name="TimeOut">Amount of time in ms to wait for the file</param>
        /// <returns>a string containing the file&#039;s contents</returns>
        public static string GetFileContents(string FileName, int TimeOut)
        {
            StreamReader Reader = null;
            int StartTime = System.Environment.TickCount;
            try
            {
                bool Opened = false;
                while (!Opened)
                {
                    try
                    {
                        if (System.Environment.TickCount - StartTime >= TimeOut)
                            throw new System.IO.IOException("File opening timed out");
                        Reader = File.OpenText(FileName);
                        Opened = true;
                    }
                    catch (System.IO.IOException e)
                    {
                        throw e;
                    }
                }
                string Contents = Reader.ReadToEnd();
                Reader.Close();
                return Contents;
            }
            catch
            {
                return "";
            }
            finally
            {
                if (Reader != null)
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
        }

    }
    /// <summary>
    /// Helps with serializing an object to XML and back again.
    /// </summary>
    public static class Serialization
    {


        /// <summary>
        /// Takes an XML file and exports the Object it holds
        /// </summary>
        /// <param name="FileName">File name to use</param>
        /// <param name="Object">Object to export to</param>
        public static void XMLToObject<T>(string FileName, out T Object)
        {
            if (string.IsNullOrEmpty(FileName))
            {
                throw new ArgumentException("File name can not be null/empty");
            }
            try
            {
                string FileContent = FileManager.GetFileContents(FileName);
                Object = XMLToObject<T>(FileContent);
            }
            catch { throw; }
        }

        /// <summary>
        /// Converts an XML string to an object
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="XML">XML string</param>
        /// <returns>The object of the specified type</returns>
        public static T XMLToObject<T>(string XML)
        {
            if (string.IsNullOrEmpty(XML))
            {
                throw new ArgumentException("XML can not be null/empty");
            }
            try
            {
                using (MemoryStream Stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(XML)))
                {
                    XmlSerializer Serializer = new XmlSerializer(typeof(T));
                    return (T)Serializer.Deserialize(Stream);
                }
            }
            catch { throw; }
        }
    }
}

   
     


XmlRootAttribute

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class MainClass {      //new products object
    public static void Main() {
        XmlAttributes attrs = new XmlAttributes();
        attrs.XmlElements.Add(new XmlElementAttribute("Book", typeof(BookProduct)));
        attrs.XmlElements.Add(new XmlElementAttribute("Product", typeof(Product)));
        XmlAttributeOverrides attrOver = new XmlAttributeOverrides();
        attrOver.Add(typeof(Inventory), "InventoryItems", attrs);
        Product newProd = new Product();
        BookProduct newBook = new BookProduct();

        newProd.ProductID = 100;
        newProd.ProductName = "Product";
        newProd.SupplierID = 10;

        newBook.ProductID = 101;
        newBook.ProductName = "New Product";
        newBook.SupplierID = 10;
        newBook.ISBN = "123456789";

        Product[] addProd = { newProd, newBook };

        Inventory inv = new Inventory();
        inv.InventoryItems = addProd;
        TextWriter tr = new StreamWriter("inventory.xml");
        XmlSerializer sr = new XmlSerializer(typeof(Inventory), attrOver);

        sr.Serialize(tr, inv);
        tr.Close();
    }

}

[System.Xml.Serialization.XmlRootAttribute()]
public class Product {
    private int prodId;
    private string prodName;
    private int suppId;
    private int catId;
    private string qtyPerUnit;
    private Decimal unitPrice;
    private short unitsInStock;
    private short unitsOnOrder;
    private short reorderLvl;
    private bool discont;
    private int disc;

    [XmlAttributeAttribute(AttributeName = "Discount")]
    public int Discount {
        get { return disc; }
        set { disc = value; }
    }

    [XmlElementAttribute()]
    public int ProductID {
        get { return prodId; }
        set { prodId = value; }
    }
    [XmlElementAttribute()]
    public string ProductName {
        get { return prodName; }
        set { prodName = value; }
    }
    [XmlElementAttribute()]
    public int SupplierID {
        get { return suppId; }
        set { suppId = value; }
    }

    [XmlElementAttribute()]
    public int CategoryID {
        get { return catId; }
        set { catId = value; }
    }

    [XmlElementAttribute()]
    public string QuantityPerUnit {
        get { return qtyPerUnit; }
        set { qtyPerUnit = value; }
    }

    [XmlElementAttribute()]
    public Decimal UnitPrice {
        get { return unitPrice; }
        set { unitPrice = value; }
    }

    [XmlElementAttribute()]
    public short UnitsInStock {
        get { return unitsInStock; }
        set { unitsInStock = value; }
    }

    [XmlElementAttribute()]
    public short UnitsOnOrder {
        get { return unitsOnOrder; }
        set { unitsOnOrder = value; }
    }

    [XmlElementAttribute()]
    public short ReorderLevel {
        get { return reorderLvl; }
        set { reorderLvl = value; }
    }

    [XmlElementAttribute()]
    public bool Discontinued {
        get { return discont; }
        set { discont = value; }
    }
}
public class Inventory {

    private Product[] stuff;
    public Inventory() { }
    [XmlArrayItem("Prod", typeof(Product)),
    XmlArrayItem("Book", typeof(BookProduct))]

    public Product[] InventoryItems {
        get { return stuff; }
        set { stuff = value; }
    }
}

public class BookProduct : Product {
    private string isbnNum;

    public BookProduct() { }

    public string ISBN {
        get { return isbnNum; }
        set { isbnNum = value; }
    }
}