- downloads.zip
- Downloads
- BusinessObjectGenerator
- GeneratedFiles
- Sample
- src
- BusinessObjectGenerator
- App_Code
- App_Data
- TheProperties_Data
- Default.aspx
- Default.aspx.cs
- Default_Console.aspx
- Default_Console.aspx.cs
- images
- background_label.gif
- logo.gif
- InvoiceClassDef.xml
- InvoiceClassDef.xsd
- InvoiceClassDef.xsx
- PropertyAndUIPropertyMaps.xml
- PropertyControls
- References
- EntLibHelper.dll
- Microsoft.Practices.EnterpriseLibrary.Caching.dll
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
- Microsoft.Practices.EnterpriseLibrary.Logging.dll
- Microsoft.Practices.ObjectBuilder.dll
- Providers.dll
- Providers.dll.refresh
- SmartCodeGen.ImplementedProviders.dll
- SmartCodeGen.ImplementedProviders.dll.refresh
- SmartCodeGen.WebUtil.dll
- SmartCodeGen.WebUtil.dll.refresh
- scgxsd_console.exe_
- styles
- Templates
- Web.Config
- BusinessObjects.Templates
- src.gpState
- src.sln
- src.suo
- SmartCodeGeneratorXsdConsole
- downloads-noexe.zip
|
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
namespace SmartCodeGeneratorXsdConsole
{
class Program
{
//
// SmartCodeGenerator Xsd Console application to generate C# Class from xsd schema.
//
// Command line: <schema>.xsd <filename>
// Example: scgxsd_console c:\students.xsd c:\students.cs
//
private static String _xsdpath;
private static String _filename;
static void Main(string[] args)
{
ParseArgs(args);
try
{
if (_xsdpath == string.Empty)
throw new Exception("Invalid Schema, Cannot find Schema.");
if (_xsdpath == string.Empty)
throw new Exception("Invalid Filename to Generate.");
Program p = new Program();
p.Generate();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(@"
// SmartCodeGenerator Xsd Console application to generate C# Class from xsd schema.
//
// Command line: <schema>.xsd <filename>
// Example: scgxsd_console c:\students.xsd c:\students.cs
//");
//
}
}
public void Generate()
{
//string xsdFileName = _xsdpath;
//string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
//string path = @"C:\DotNetWorkFolder2005\Riccardo\TestProject\AJAXEnabledWebApplication2\AJAXEnabledWebApplication2";
string xsdPath = _xsdpath;//Path.Combine(path, xsdFileName);
// load the xsd file
XmlSchema xsd;
using(FileStream stream = new FileStream(xsdPath, FileMode.Open, FileAccess.Read))
{
xsd = XmlSchema.Read(stream, null);
}
Console.WriteLine("xsd.IsCompiled {0}", xsd.IsCompiled);
//add xsd to the collection of XmlSchema
XmlSchemas xsds = new XmlSchemas();
xsds.Add(xsd);
//compile the xsd
xsds.Compile(null, true);
//create instance of XmlSchemaImporter
XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsds);
// create the codedom
CodeNamespace codeNamespace = new CodeNamespace("SmartCodeGen");
// crate XmlCodeExpoerter
XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace);
List<object> maps = new List<object>();
foreach(XmlSchemaType schemaType in xsd.SchemaTypes.Values)
{
maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName));
}
foreach(XmlSchemaElement schemaElement in xsd.Elements.Values)
{
maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
}
foreach(XmlTypeMapping map in maps)
{
codeExporter.ExportTypeMapping(map);
}
// Check for invalid characters in identifiers
CodeGenerator.ValidateIdentifiers(codeNamespace);
// output the C# code
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
using(StringWriter writer = new StringWriter())
{
codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions());
string s = (writer.GetStringBuilder().ToString());
using (StreamWriter sw = File.CreateText(_filename))
{
sw.Write(s);
}
}
Console.WriteLine("C# Class Created Successfully.");
}
private static void ParseArgs(String[] args) {
try {
if (args.Length >= 1)
_xsdpath = args[0];
if (args.Length >= 2)
_filename = args[1];
}
catch {
}
if (_xsdpath == null)
_xsdpath = string.Empty;
if (_filename == null)
_filename = string.Empty;
}
}
}
|
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.