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

Serializing XML with stylesheet

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
25 Apr 20063 min read 65.3K   423   26  
An explanation of how to serialize an object and include an XML-stylesheet processing instruction.
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.Reflection;


namespace WriteXmlWithStyle
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private string ApplicationDir
        {
            get
            {
                Assembly assy = Assembly.GetExecutingAssembly();

                FileInfo finfo = new FileInfo(assy.FullName);

                return finfo.Directory.Parent.Parent.FullName;
            }
        }

        private void WriteXmlBtn_Click(object sender, EventArgs e)
        {
            DictionarySerializer serializer = new DictionarySerializer();

            Dictionary dictionary = serializer.CreateTestDictionary();

            // Write the dictionary out to a file without a stylesheet reference
            string xmlFile = Path.Combine(ApplicationDir, "Dictionary.xml");
            File.Delete(xmlFile);

            FileStream stream = File.OpenWrite(xmlFile);
            try
            {
                serializer.Serialize(dictionary, stream, null);
            }
            finally
            {
                stream.Close();
            }

            // Now write the dictionary out with a stylesheet reference
            xmlFile = Path.Combine(ApplicationDir, "DictionaryWithStyle.xml");
            File.Delete(xmlFile);

            stream = File.OpenWrite(xmlFile);
            try
            {
                serializer.Serialize(dictionary, stream, "Dictionary.xslt");
            }
            finally
            {
                stream.Close();
            }
        }
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
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