本文整理汇总了C#中System.Xml.Serialization.XmlSchemaImporter.ImportSchemaType方法的典型用法代码示例。如果您正苦于以下问题:C# XmlSchemaImporter.ImportSchemaType方法的具体用法?C# XmlSchemaImporter.ImportSchemaType怎么用?C# XmlSchemaImporter.ImportSchemaType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.XmlSchemaImporter
的用法示例。
在下文中一共展示了XmlSchemaImporter.ImportSchemaType方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main(string[] args)
{
XmlSchema rootSchema = GetSchemaFromFile("fpml-main-4-2.xsd");
var schemaSet = new List<XmlSchemaExternal>();
ExtractIncludes(rootSchema, ref schemaSet);
var schemas = new XmlSchemas { rootSchema };
schemaSet.ForEach(schemaExternal => schemas.Add(GetSchemaFromFile(schemaExternal.SchemaLocation)));
schemas.Compile(null, true);
var xmlSchemaImporter = new XmlSchemaImporter(schemas);
var codeNamespace = new CodeNamespace("Hosca.FpML4_2");
var xmlCodeExporter = new XmlCodeExporter(codeNamespace);
var xmlTypeMappings = new List<XmlTypeMapping>();
foreach (XmlSchemaType schemaType in rootSchema.SchemaTypes.Values)
xmlTypeMappings.Add(xmlSchemaImporter.ImportSchemaType(schemaType.QualifiedName));
foreach (XmlSchemaElement schemaElement in rootSchema.Elements.Values)
xmlTypeMappings.Add(xmlSchemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
xmlTypeMappings.ForEach(xmlCodeExporter.ExportTypeMapping);
CodeGenerator.ValidateIdentifiers(codeNamespace);
foreach (CodeTypeDeclaration codeTypeDeclaration in codeNamespace.Types)
{
for (int i = codeTypeDeclaration.CustomAttributes.Count - 1; i >= 0; i--)
{
CodeAttributeDeclaration cad = codeTypeDeclaration.CustomAttributes[i];
if (cad.Name == "System.CodeDom.Compiler.GeneratedCodeAttribute")
codeTypeDeclaration.CustomAttributes.RemoveAt(i);
}
}
using (var writer = new StringWriter())
{
new CSharpCodeProvider().GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions());
//Console.WriteLine(writer.GetStringBuilder().ToString());
File.WriteAllText(Path.Combine(rootFolder, "FpML4_2.Generated.cs"), writer.GetStringBuilder().ToString());
}
Console.ReadLine();
}
示例2: GeneratedClassFromStream
private CodeNamespace GeneratedClassFromStream(Stream stream, string nameSpace)
{
XmlSchema xsd;
stream.Seek(0, SeekOrigin.Begin);
using (stream)
{
xsd = XmlSchema.Read(stream, null);
}
XmlSchemas xsds = new XmlSchemas();
xsds.Add(xsd);
xsds.Compile(null, true);
XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsds);
// create the codedom
CodeNamespace codeNamespace = new CodeNamespace(nameSpace);
XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace);
List<XmlTypeMapping> maps = new List<XmlTypeMapping>();
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);
}
this.RemoveUnusedStuff(codeNamespace);
return codeNamespace;
}
示例3: Process
/// <summary>
/// Initiate code generation process
/// </summary>
/// <param name="generatorParams">Generator parameters</param>
/// <returns></returns>
internal static Result<CodeNamespace> Process(GeneratorParams generatorParams)
{
var ns = new CodeNamespace();
XmlReader reader = null;
try
{
#region Set generation context
GeneratorContext.GeneratorParams = generatorParams;
#endregion
#region Get XmlTypeMapping
XmlSchema xsd;
var schemas = new XmlSchemas();
reader = XmlReader.Create(generatorParams.InputFilePath);
xsd = XmlSchema.Read(reader, new ValidationEventHandler(Validate));
var schemaSet = new XmlSchemaSet();
schemaSet.Add(xsd);
schemaSet.Compile();
foreach (XmlSchema schema in schemaSet.Schemas())
{
schemas.Add(schema);
}
var exporter = new XmlCodeExporter(ns);
var generationOptions = CodeGenerationOptions.None;
if (generatorParams.Serialization.GenerateOrderXmlAttributes)
{
generationOptions = CodeGenerationOptions.GenerateOrder;
}
var importer = new XmlSchemaImporter(schemas, generationOptions, new ImportContext(new CodeIdentifiers(), false));
foreach (XmlSchemaElement element in xsd.Elements.Values)
{
var mapping = importer.ImportTypeMapping(element.QualifiedName);
exporter.ExportTypeMapping(mapping);
}
//Fixes/handles http://xsd2code.codeplex.com/WorkItem/View.aspx?WorkItemId=6941
foreach (XmlSchemaType type in xsd.Items.OfType<XmlSchemaType>())
{
var mapping = importer.ImportSchemaType(type.QualifiedName);
exporter.ExportTypeMapping(mapping);
}
#endregion
#region Execute extensions
var getExtensionResult = GeneratorFactory.GetCodeExtension(generatorParams);
if (!getExtensionResult.Success) return new Result<CodeNamespace>(ns, false, getExtensionResult.Messages);
var ext = getExtensionResult.Entity;
ext.Process(ns, xsd);
#endregion Execute extensions
return new Result<CodeNamespace>(ns, true);
}
catch (Exception e)
{
return new Result<CodeNamespace>(ns, false, e.Message, MessageType.Error);
}
finally
{
if (reader != null)
reader.Close();
}
}
示例4: GenerateCode
/// <summary>
/// Generates the data contracts for given xsd file(s).
/// </summary>
public CodeNamespace GenerateCode()
{
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = new CodeNamespace(options.ClrNamespace);
codeCompileUnit.Namespaces.Add(codeNamespace);
// Build the code generation options.
GenerationOptions generationOptions = GenerationOptions.None;
if (options.GenerateProperties)
{
generationOptions |= GenerationOptions.GenerateProperties;
}
if (options.EnableDataBinding)
{
generationOptions |= GenerationOptions.EnableDataBinding;
}
if (options.GenerateOrderIdentifiers)
{
generationOptions |= GenerationOptions.GenerateOrder;
}
// Build the CodeDom object graph.
XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace, codeCompileUnit, generationOptions, null);
CodeIdentifiers codeIdentifiers = new CodeIdentifiers();
ImportContext importContext = new ImportContext(codeIdentifiers, false);
XmlSchemaImporter schemaimporter = new XmlSchemaImporter(schemas, generationOptions, codeProvider, importContext);
for (int si = 0; si < schemas.Count; si++)
{
XmlSchema schema = schemas[si];
IEnumerator enumerator = schema.Elements.Values.GetEnumerator();
IEnumerator enumerator2 = schema.SchemaTypes.Values.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
XmlSchemaElement element = (XmlSchemaElement)enumerator.Current;
if (element.IsAbstract) continue;
XmlTypeMapping typemapping = schemaimporter.ImportTypeMapping(element.QualifiedName);
codeExporter.ExportTypeMapping(typemapping);
}
while (enumerator2.MoveNext())
{
XmlSchemaType type = (XmlSchemaType)enumerator2.Current;
if (CouldBeAnArray(type)) continue;
XmlTypeMapping typemapping = schemaimporter.ImportSchemaType(type.QualifiedName);
codeExporter.ExportTypeMapping(typemapping);
}
}
finally
{
IDisposable disposableobject = enumerator as IDisposable;
if (disposableobject != null)
{
disposableobject.Dispose();
}
IDisposable disposableobject2 = enumerator2 as IDisposable;
if (disposableobject2 != null)
{
disposableobject2.Dispose();
}
}
}
if (codeNamespace.Types.Count == 0)
{
throw new Exception("No types were generated.");
}
return codeNamespace;
}
示例5: ImportWildcardElementAsClass
public void ImportWildcardElementAsClass ()
{
var xss = new XmlSchemas ();
xss.Add (XmlSchema.Read (XmlReader.Create ("Test/XmlFiles/xsd/670945-1.xsd"), null));
xss.Add (XmlSchema.Read (XmlReader.Create ("Test/XmlFiles/xsd/670945-2.xsd"), null));
var imp = new XmlSchemaImporter (xss);
var xtm = imp.ImportSchemaType (new XmlQualifiedName ("SystemDateTime", "http://www.onvif.org/ver10/schema"));
var cns = new CodeNamespace ();
var exp = new XmlCodeExporter (cns);
exp.ExportTypeMapping (xtm);
var sw = new StringWriter ();
new CSharpCodeProvider ().GenerateCodeFromNamespace (cns, sw, null);
Assert.IsTrue (sw.ToString ().IndexOf ("class SystemDateTimeExtension") > 0, "#1");
}
示例6: Program
public Program(params string[] args)
{
XmlSchemas xsds = new XmlSchemas();
var i = 0;
while (!args[i].StartsWith("/o:") || i >= args.Length)
{
xsds.Add(GetSchema(args[i]));
i++;
}
var output = string.Empty;
if (args[i].StartsWith("/o:"))
{
output = args[i].Substring(3);
}
xsds.Compile(null, true);
XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsds, CodeGenerationOptions.GenerateOrder, null);
// create the codedom
var codeNamespace = new CodeNamespace("QbSync.QbXml.Objects");
codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Linq"));
CodeCompileUnit compileUnit = new CodeCompileUnit();
compileUnit.Namespaces.Add(codeNamespace);
var codeExporter = new XmlCodeExporter(codeNamespace, compileUnit, CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateOrder);
foreach (XmlSchema xsd in xsds)
{
ArrayList maps = new ArrayList();
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);
}
}
var typeEnhancer = new TypeEnhancer(codeNamespace, xsds);
typeEnhancer.Enhance();
// Add a comment at the top of the file
var x = fileComment.Split('\n').Select(m => new CodeCommentStatement(m)).ToArray();
codeNamespace.Comments.AddRange(x);
// 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
{
BlankLinesBetweenMembers = true,
BracingStyle = "C",
ElseOnClosing = true,
IndentString = " "
});
string content = writer.GetStringBuilder().ToString();
if (string.IsNullOrEmpty(output))
{
Console.WriteLine(content);
Console.ReadLine();
}
else
{
File.WriteAllText(output, content);
}
}
}
示例7: resolveParticle
void resolveParticle (XmlSchemaImporter schema_importer,
XmlSchemaParticle particle,
List<MessagePartDescription> parts,
string ns,
int depth)
{
if (particle is XmlSchemaGroupBase) {
//sequence,
//FIXME: others?
if (depth <= 0)
return;
XmlSchemaGroupBase groupBase = particle as XmlSchemaGroupBase;
foreach (XmlSchemaParticle item in groupBase.Items)
resolveParticle (schema_importer, item, parts, ns, depth - 1);
return;
}
XmlSchemaElement elem = particle as XmlSchemaElement;
if (elem == null)
return;
MessagePartDescription msg_part = null;
XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
if (ct == null) {
//Not a complex type
XmlSchemaSimpleType simple = elem.ElementSchemaType as XmlSchemaSimpleType;
msg_part = new MessagePartDescription (
elem.Name, ns);
if (elem.SchemaType != null)
msg_part.XmlTypeMapping = schema_importer.ImportTypeMapping (elem.QualifiedName);
else
msg_part.XmlTypeMapping = schema_importer.ImportSchemaType (elem.SchemaTypeName);
msg_part.TypeName = new QName (GetCLRTypeName (elem.SchemaTypeName), "");
parts.Add (msg_part);
return;
}
if (depth > 0) {
resolveParticle (schema_importer, ct.ContentTypeParticle, parts, ns, depth - 1);
return;
}
//depth <= 0
msg_part = new MessagePartDescription (elem.Name, ns);
if (elem.SchemaType != null)
msg_part.XmlTypeMapping = schema_importer.ImportTypeMapping (elem.QualifiedName);
else
msg_part.XmlTypeMapping = schema_importer.ImportSchemaType (elem.SchemaTypeName);
msg_part.TypeName = elem.SchemaTypeName;
parts.Add (msg_part);
}
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:56,代码来源:DataContractSerializerMessageContractImporter.cs
示例8: Generate
public void Generate(Stream xsdInput, TextWriter output)
{
if (Options == null)
Options = new XsdCodeGeneratorOptions
{
UseLists = true,
CapitalizeProperties = true,
StripDebuggerStepThroughAttribute = true,
OutputNamespace = "Xsd2",
UseNullableTypes = true
};
if (Options.Imports != null)
{
foreach (var import in Options.Imports)
{
if (File.Exists(import))
{
ImportImportedSchema(import);
}
else if (Directory.Exists(import))
{
foreach (var file in Directory.GetFiles("*.xsd"))
ImportImportedSchema(file);
}
else
{
throw new InvalidOperationException(String.Format("Import '{0}' is not a file nor a directory.", import));
}
}
}
XmlSchema xsd = XmlSchema.Read(xsdInput, null);
xsds.Add(xsd);
xsds.Compile(null, true);
XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsds);
// create the codedom
CodeNamespace codeNamespace = new CodeNamespace(Options.OutputNamespace);
XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace);
List<XmlTypeMapping> maps = new List<XmlTypeMapping>();
foreach (XmlSchemaElement schemaElement in xsd.Elements.Values)
{
if (!ElementBelongsToImportedSchema(schemaElement))
maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
}
foreach (XmlSchemaComplexType schemaElement in xsd.Items.OfType<XmlSchemaComplexType>())
{
maps.Add(schemaImporter.ImportSchemaType(schemaElement.QualifiedName));
}
foreach (XmlSchemaSimpleType schemaElement in xsd.Items.OfType<XmlSchemaSimpleType>())
{
maps.Add(schemaImporter.ImportSchemaType(schemaElement.QualifiedName));
}
foreach (XmlTypeMapping map in maps)
{
codeExporter.ExportTypeMapping(map);
}
ImproveCodeDom(codeNamespace, xsd);
if (OnValidateGeneratedCode != null)
OnValidateGeneratedCode(codeNamespace, xsd);
// Check for invalid characters in identifiers
CodeGenerator.ValidateIdentifiers(codeNamespace);
// output the C# code
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
codeProvider.GenerateCodeFromNamespace(codeNamespace, output, new CodeGeneratorOptions());
}
示例9: GenerateForComplexTypes
/// <summary>
/// Generate code for all of the complex types in the schema
/// </summary>
/// <param name="xsd"></param>
/// <param name="importer"></param>
/// <param name="exporter"></param>
private void GenerateForComplexTypes(
XmlSchema xsd,
XmlSchemaImporter importer,
XmlCodeExporter exporter)
{
foreach (XmlSchemaObject type in xsd.SchemaTypes.Values)
{
XmlSchemaComplexType ct = type as XmlSchemaComplexType;
if (ct != null)
{
Trace.TraceInformation("Generating for Complex Type: {0}", ct.Name);
XmlTypeMapping mapping = importer.ImportSchemaType(ct.QualifiedName);
exporter.ExportTypeMapping(mapping);
}
}
}