Click here to Skip to main content
12,510,818 members (48,766 online)
Click here to Skip to main content

Stats

21K views
287 downloads
42 bookmarked
Posted

Document and Code Generation by LINQ and XSL

, 3 Aug 2008 CPOL
An article on how to generate source code as well as populate Excel Spreadsheets.
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;
using Transform;

namespace LinqCodeGeneration
{
	class Program
	{
		static void Main()
		{
			Test_1();
			//Test_2();
			//Test_3();

			//TestCode.Print();
            
			//Final();

			Console.Out.WriteLine("\n\n\nPress Enter To Continue...");
			Console.In.ReadLine();
		}

		/// <summary>
		/// TEST #1
		/// </summary>
		public static void Test_1()
		{
			DataTable dataTable = TestData.MyDataTable();

			XElement dataScript = LinqData_1.CreateClass(dataTable);
			XElement xsltScript = Xslt_1.ClassTransform();

			Test(1, xsltScript, dataScript);
		}

		/// <summary>
		/// TEST #2
		/// </summary>
		public static void Test_2()
		{
			DataTable dataTable = TestData.MyDataTable();

			XElement dataScript = LinqData_2.CreateClass(dataTable);
			XElement xsltScript = Xslt_2.ClassTransform();

			Test(2, xsltScript, dataScript);
		}

		/// <summary>
		/// TEST #3
		/// </summary>
		public static void Test_3()
		{
			DataTable dataTable = TestData.MyDataTable();

			XElement dataScript = LinqData_3.CreateClass(dataTable);
			XElement xsltScript = Xslt_3.ClassTransform();

			Test(3, xsltScript, dataScript);
		}

		/// <summary>
		/// Final
		/// </summary>
		public static void Final()
		{
			DataTable dataTable = TestData.MyDataTable();

			XElement dataScript = LinqData_Final.CreateClass_Final(dataTable);
			XElement xsltScript = Xslt_Final.ClassTransform_Final();

			Test("Final", xsltScript, dataScript);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="testNumber"></param>
		private static void PrintHeader(object testNumber)
		{
			Console.Out.WriteLine("\n\n\n");
			Console.Out.WriteLine("=========================");
			if ( testNumber is int)
				Console.Out.WriteLine("    Test #" + testNumber.ToString().PadLeft(2, '0'));
			else
				Console.Out.WriteLine("    " + testNumber);
			Console.Out.WriteLine("=========================");
			Console.Out.WriteLine();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="testNumber"></param>
		/// <param name="xsltScript"></param>
		/// <param name="dataScript"></param>
		public static void Test(object testNumber, XElement xsltScript, XElement dataScript)
		{
			try
			{
				PrintHeader(testNumber);

				StringBuilder sb = new StringBuilder(dataScript.ToString());
				StreamWriter sw = new StreamWriter("output_" + testNumber + ".txt", false);
				sw.Write(sb.ToString());
				sw.Close();

				// Enable scripting
				XsltSettings settings = new XsltSettings(true, true);

				// Create the XslTransform
				XslCompiledTransform xslt = new XslCompiledTransform();
				xslt.Load(xsltScript.CreateReader(), settings, null);

				// Load the data
				XPathDocument doc = new XPathDocument(dataScript.CreateReader());

				// Output to console using XmlTextWriter
				XmlTextWriter writer = new XmlTextWriter(Console.Out);
				writer.Formatting = Formatting.Indented;

				// Transform the data with the XSLT stylesheet
				xslt.Transform(doc.CreateNavigator(), writer);
				writer.Close();
			}
			catch (Exception ex)
			{
				System.Diagnostics.Debug.WriteLine(ex.Message);
			}
		}
	}
}

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)

Share

About the Author

Dave Elliott
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 3 Aug 2008
Article Copyright 2008 by Dave Elliott
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid