Click here to Skip to main content
11,714,776 members (77,068 online)
Click here to Skip to main content
Add your own
alternative version

Document and Code Generation by LINQ and XSL

, 3 Aug 2008 CPOL 19.8K 276 42
An article on how to generate source code as well as populate Excel Spreadsheets.
LINQ_Code_and_Doc_Generation.zip
LINQ Code and Doc Generation
CallExternalDll
Properties
ContactList
My Project
Application.myapp
Settings.settings
CreateBilling
Properties
Documents
My Project
Application.myapp
Settings.settings
Sample Docs
Invoice
docProps
xl
drawings
_rels
drawing1.xml.rels
media
image1.png
printerSettings
printerSettings1.bin
theme
worksheets
_rels
sheet1.xml.rels
_rels
workbook.xml.rels
_rels
.rels
Invoice.xlsx
LinqCodeGeneration
Properties
Some.Other.Library
Properties
Transform
Final
My Project
Application.myapp
Settings.settings
Test_1
Test_2
Test_3
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...

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