本文整理汇总了C#中System.Xml.Schema.XmlSchema.Compile方法的典型用法代码示例。如果您正苦于以下问题:C# XmlSchema.Compile方法的具体用法?C# XmlSchema.Compile怎么用?C# XmlSchema.Compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Schema.XmlSchema
的用法示例。
在下文中一共展示了XmlSchema.Compile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAdd
public void TestAdd ()
{
XmlSchemaCollection col = new XmlSchemaCollection ();
XmlSchema schema = new XmlSchema ();
XmlSchemaElement elem = new XmlSchemaElement ();
elem.Name = "foo";
schema.Items.Add (elem);
schema.TargetNamespace = "urn:foo";
col.Add (schema);
col.Add (schema); // No problem !?
XmlSchema schema2 = new XmlSchema ();
schema2.Items.Add (elem);
schema2.TargetNamespace = "urn:foo";
col.Add (schema2); // No problem !!
schema.Compile (null);
col.Add (schema);
col.Add (schema); // Still no problem !!!
schema2.Compile (null);
col.Add (schema2);
schema = GetSchema ("Test/XmlFiles/xsd/3.xsd");
schema.Compile (null);
col.Add (schema);
schema2 = GetSchema ("Test/XmlFiles/xsd/3.xsd");
schema2.Compile (null);
col.Add (schema2);
}
示例2: CreateSimpletypeLength
private void CreateSimpletypeLength(string length, string minLength, string maxLength, bool expected) {
passed = true;
XmlSchema schema = new XmlSchema();
XmlSchemaSimpleType testType = new XmlSchemaSimpleType();
testType.Name = "TestType";
XmlSchemaSimpleTypeRestriction testTypeRestriction = new XmlSchemaSimpleTypeRestriction();
testTypeRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
if (length != "-") {
XmlSchemaLengthFacet _length = new XmlSchemaLengthFacet();
_length.Value = length;
testTypeRestriction.Facets.Add(_length);
}
if (minLength != "-") {
XmlSchemaMinLengthFacet _minLength = new XmlSchemaMinLengthFacet();
_minLength.Value = minLength;
testTypeRestriction.Facets.Add(_minLength);
}
if (maxLength != "-") {
XmlSchemaMaxLengthFacet _maxLength = new XmlSchemaMaxLengthFacet();
_maxLength.Value = maxLength;
testTypeRestriction.Facets.Add(_maxLength);
}
testType.Content = testTypeRestriction;
schema.Items.Add(testType);
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
Assert.IsTrue (expected == passed, (passed ? "Test passed, should have failed" : "Test failed, should have passed") + ": " + length + " " + minLength + " " + maxLength);
}
示例3: SetUpFixture
public void SetUpFixture()
{
string relativePath = "../../../TestFiles/log4net.xsd";
Assert.IsTrue(File.Exists(relativePath), string.Format("File does not exist {0}", relativePath));
xmlSchema = XmlSchema.Read(new XmlTextReader(relativePath), null);
xmlSchema.Compile(null);
provider = new SchemaParameterDescriptorProvider(xmlSchema);
}
示例4: LoadSchema
private static XmlSchema LoadSchema()
{
var schema = new XmlSchema();
using (var stream = typeof(XmlCompletionDataProvider).Assembly.GetManifestResourceStream("InnovatorAdmin.Editor.xslt.xsd"))
{
using (var reader = XmlReader.Create(stream))
{
schema = XmlSchema.Read(reader, new ValidationEventHandler(SchemaValidation));
schema.Compile(new ValidationEventHandler(SchemaValidation));
}
}
return schema;
}
示例5: WhiteSpaceTest
/* Takes a type name, and a valid example of that type,
Creates a schema consisting of a single element of that
type, and validates a bit of xml containing the valid
value, with whitespace against that schema.
FIXME: Really we want to test the value of whitespace more
directly that by creating a schema then parsing a string.
*/
public void WhiteSpaceTest(string type, string valid) {
passed = true;
XmlSchema schema = new XmlSchema();
schema.TargetNamespace= "http://example.com/testCase";
XmlSchemaElement element = new XmlSchemaElement();
element.Name = "a";
element.SchemaTypeName = new XmlQualifiedName(type, "http://www.w3.org/2001/XMLSchema");
schema.Items.Add(element);
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(new StringReader("<a xmlns='http://example.com/testCase'>\n\n"+valid+"\n\n</a>" )));
vr.Schemas.Add(schema);
vr.ValidationType = ValidationType.Schema;
// vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
while(vr.Read()) { };
vr.Close();
Assert(type + " doesn't collapse whitespace: " + (errorInfo != null ? errorInfo.Message : null), passed);
}
示例6: Main
public static void Main (string[] args)
{
if (args.Length < 2) {
PrintUsage ();
}
Stream s = null;
switch (args[0]) {
case "ecma":
s = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("monodoc-ecma.xsd");
break;
default:
Console.WriteLine ("Unknown provider: {0}", args[0]);
Environment.Exit (0);
break;
}
if (s == null) {
Console.WriteLine ("ERROR: schema for {0} was not found", args[0]);
Environment.Exit (1);
}
schema = XmlSchema.Read (s, null);
schema.Compile (null);
// skip args[0] because it is the provider name
for (int i = 1; i < args.Length; i++) {
string arg = args[i];
if (IsMonodocFile (arg))
ValidateFile (arg);
if (Directory.Exists (arg))
{
RecurseDirectory (arg);
}
}
Console.WriteLine ("Total validation errors: {0}", errors);
}
示例7: XssFilterInfo
internal XssFilterInfo(string schemaLocation)
{
XmlDocument schemaDoc = GetSchemaDoc(schemaLocation);
XmlReader schemaReader = new XmlNodeReader(schemaDoc);
try
{
Schema = XmlSchema.Read(schemaReader, null);
}
finally
{
schemaReader.Close();
}
Schema.Compile(null);
if (Type.GetType("Mono.Runtime", false) != null)
{
UriAndStyleValidator = new AttributeValueValidator();
UriAndStyleValidator.Add(schemaDoc, Schema, "URI", Schema.TargetNamespace);
UriAndStyleValidator.Add(schemaDoc, Schema, "InlineStyle", Schema.TargetNamespace);
// NOTE: We don't bother with attributes of type "URIs" (plural) because only <object> has such an
// attribute and we don't allow that element.
}
}
示例8: XmlSchemaDataImporter
static XmlSchemaDataImporter ()
{
XmlSchema s = new XmlSchema ();
XmlSchemaAttribute a = new XmlSchemaAttribute ();
a.Name = "foo";
a.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
s.Items.Add (a);
XmlSchemaAttribute b = new XmlSchemaAttribute ();
b.Name = "bar";
b.SchemaTypeName = new XmlQualifiedName ("decimal", XmlSchema.Namespace);
s.Items.Add (b);
XmlSchemaElement e = new XmlSchemaElement ();
e.Name = "bar";
s.Items.Add (e);
s.Compile (null);
schemaIntegerType = a.AttributeType as XmlSchemaDatatype;
schemaDecimalType = b.AttributeType as XmlSchemaDatatype;
schemaAnyType = e.ElementType as XmlSchemaComplexType;
}
示例9: Find
object Find (XmlSchema schema, XmlQualifiedName name, Type type)
{
if (!schema.IsCompiled) {
schema.Compile (null);
}
XmlSchemaObjectTable tbl = null;
if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
tbl = schema.SchemaTypes;
else if (type == typeof (XmlSchemaAttribute))
tbl = schema.Attributes;
else if (type == typeof (XmlSchemaAttributeGroup))
tbl = schema.AttributeGroups;
else if (type == typeof (XmlSchemaElement))
tbl = schema.Elements;
else if (type == typeof (XmlSchemaGroup))
tbl = schema.Groups;
else if (type == typeof (XmlSchemaNotation))
tbl = schema.Notations;
object res = (tbl != null) ? tbl [name] : null;
if (res != null && res.GetType () != type) return null;
else return res;
}
示例10: ReadSchema
/// <summary>
/// Loads the schema.
/// </summary>
void ReadSchema(XmlReader reader)
{
try {
schema = XmlSchema.Read(reader, new ValidationEventHandler(SchemaValidation));
schema.Compile(new ValidationEventHandler(SchemaValidation));
namespaceUri = schema.TargetNamespace;
} finally {
reader.Close();
}
}
示例11: TestCompile_ZeroLength_TargetNamespace
public void TestCompile_ZeroLength_TargetNamespace ()
{
XmlSchema schema = new XmlSchema ();
schema.TargetNamespace = string.Empty;
Assert.IsTrue (!schema.IsCompiled);
// MS.NET 1.x: The Namespace '' is an invalid URI.
// MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value.
schema.Compile (null);
}
示例12: Generate
/// <summary>
/// Generates strongly typed serialization classes for the given schema document
/// under the given namespace and generates a code compile unit.
/// </summary>
/// <param name="xmlSchema">Schema document to generate classes for.</param>
/// <param name="generateNamespace">Namespace to be used for the generated code.</param>
/// <returns>A fully populated CodeCompileUnit, which can be serialized in the language of choice.</returns>
public static CodeCompileUnit Generate(XmlSchema xmlSchema, string generateNamespace)
{
if (xmlSchema == null)
{
throw new ArgumentNullException("xmlSchema");
}
if (generateNamespace == null)
{
throw new ArgumentNullException("generateNamespace");
}
simpleTypeNamesToClrTypeNames = new Hashtable();
typeNamesToEnumDeclarations = new Hashtable();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = new CodeNamespace(generateNamespace);
codeCompileUnit.Namespaces.Add(codeNamespace);
codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml"));
simpleTypeNamesToClrTypeNames.Add("dateTime", "DateTime");
simpleTypeNamesToClrTypeNames.Add("uint", "uint");
simpleTypeNamesToClrTypeNames.Add("int", "int");
simpleTypeNamesToClrTypeNames.Add("integer", "int");
simpleTypeNamesToClrTypeNames.Add("NMTOKEN", "string");
simpleTypeNamesToClrTypeNames.Add("nonNegativeInteger", "uint");
simpleTypeNamesToClrTypeNames.Add("string", "string");
xmlSchema.Compile(null);
foreach (XmlSchemaObject schemaObject in xmlSchema.SchemaTypes.Values)
{
XmlSchemaComplexType schemaComplexType = schemaObject as XmlSchemaComplexType;
if (schemaComplexType != null)
{
ProcessComplexType(schemaComplexType, codeNamespace);
}
XmlSchemaSimpleType schemaSimpleType = schemaObject as XmlSchemaSimpleType;
if (schemaSimpleType != null)
{
ProcessSimpleType(schemaSimpleType, codeNamespace);
}
}
foreach (XmlSchemaObject schemaObject in xmlSchema.Elements.Values)
{
XmlSchemaElement schemaElement = schemaObject as XmlSchemaElement;
if (schemaElement != null)
{
ProcessElement(schemaElement, codeNamespace);
}
}
return codeCompileUnit;
}
示例13: XsdSimpleRestrictionType
public XsdSimpleRestrictionType (RelaxngDatatype primitive, RelaxngParamList parameters)
{
type = new XmlSchemaSimpleType ();
XmlSchemaSimpleTypeRestriction r =
new XmlSchemaSimpleTypeRestriction ();
type.Content = r;
string ns = primitive.NamespaceURI;
// Remap XML Schema datatypes namespace -> XML Schema namespace.
if (ns == "http://www.w3.org/2001/XMLSchema-datatypes")
ns = XSchema.Namespace;
r.BaseTypeName = new XmlQualifiedName (primitive.Name, ns);
foreach (RelaxngParam p in parameters) {
XmlSchemaFacet f = null;
string value = p.Value;
switch (p.Name) {
case "maxExclusive":
f = new XmlSchemaMaxExclusiveFacet ();
break;
case "maxInclusive":
f = new XmlSchemaMaxInclusiveFacet ();
break;
case "minExclusive":
f = new XmlSchemaMinExclusiveFacet ();
break;
case "minInclusive":
f = new XmlSchemaMinInclusiveFacet ();
break;
case "pattern":
f = new XmlSchemaPatternFacet ();
// .NET/Mono Regex has a bug that it does not support "IsLatin-1Supplement"
// (it somehow breaks at '-').
value = value.Replace ("\\p{IsLatin-1Supplement}", "[\\x80-\\xFF]");
break;
case "whiteSpace":
f = new XmlSchemaWhiteSpaceFacet ();
break;
case "length":
f = new XmlSchemaLengthFacet ();
break;
case "maxLength":
f = new XmlSchemaMaxLengthFacet ();
break;
case "minLength":
f = new XmlSchemaMinLengthFacet ();
break;
case "fractionDigits":
f = new XmlSchemaFractionDigitsFacet ();
break;
case "totalDigits":
f = new XmlSchemaTotalDigitsFacet ();
break;
default:
throw new RelaxngException (String.Format ("XML Schema facet {0} is not recognized or not supported.", p.Name));
}
f.Value = value;
r.Facets.Add (f);
}
// Now we create XmlSchema to handle simple-type
// based validation (since there is no other way,
// because of sucky XmlSchemaSimpleType design).
schema = new XSchema ();
XmlSchemaElement el = new XmlSchemaElement ();
el.Name = "root";
el.SchemaType = type;
schema.Items.Add (el);
schema.Compile (null);
}
示例14: Generate
/// <summary>
/// Generates strongly typed serialization classes for the given schema document
/// under the given namespace and generates a code compile unit.
/// </summary>
/// <param name="xmlSchema">Schema document to generate classes for.</param>
/// <param name="generateNamespace">Namespace to be used for the generated code.</param>
/// <param name="commonNamespace">Namespace in which to find common classes and interfaces,
/// like ISchemaElement.</param>
/// <returns>A fully populated CodeCompileUnit, which can be serialized in the language of choice.</returns>
public static CodeCompileUnit Generate(XmlSchema xmlSchema, string generateNamespace, string commonNamespace)
{
if (xmlSchema == null)
{
throw new ArgumentNullException("xmlSchema");
}
if (generateNamespace == null)
{
throw new ArgumentNullException("generateNamespace");
}
simpleTypeNamesToClrTypeNames = new Hashtable();
typeNamesToEnumDeclarations = new Dictionary<string,EnumDeclaration>();
refToAttributeGroups = new Dictionary<string, XmlSchemaAttributeGroup>();
enumsToParseMethodClasses = new Dictionary<EnumDeclaration, CodeTypeDeclaration>();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = new CodeNamespace(generateNamespace);
codeCompileUnit.Namespaces.Add(codeNamespace);
codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.CodeDom.Compiler")); // for GeneratedCodeAttribute
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Diagnostics.CodeAnalysis"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Globalization"));
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml"));
if (commonNamespace != null)
{
codeNamespace.Imports.Add(new CodeNamespaceImport(commonNamespace));
}
// NOTE: This hash table serves double duty so be sure to have the XSD
// type name mapped to the CLR type name *and* the CLR type name
// mapped to the same CLR type name. Look at long and bool for
// examples below (and before you ask, no I don't know why DateTime
// just works).
simpleTypeNamesToClrTypeNames.Add("dateTime", "DateTime");
simpleTypeNamesToClrTypeNames.Add("integer", "int");
simpleTypeNamesToClrTypeNames.Add("int", "int");
simpleTypeNamesToClrTypeNames.Add("NMTOKEN", "string");
simpleTypeNamesToClrTypeNames.Add("string", "string");
simpleTypeNamesToClrTypeNames.Add("nonNegativeInteger", "long");
simpleTypeNamesToClrTypeNames.Add("long", "long");
simpleTypeNamesToClrTypeNames.Add("boolean", "bool");
simpleTypeNamesToClrTypeNames.Add("bool", "bool");
xmlSchema.Compile(null);
foreach (XmlSchemaAttributeGroup schemaAttributeGroup in xmlSchema.AttributeGroups.Values)
{
refToAttributeGroups.Add(schemaAttributeGroup.Name, schemaAttributeGroup);
}
foreach (XmlSchemaObject schemaObject in xmlSchema.SchemaTypes.Values)
{
XmlSchemaSimpleType schemaSimpleType = schemaObject as XmlSchemaSimpleType;
if (schemaSimpleType != null)
{
ProcessSimpleType(schemaSimpleType, codeNamespace);
}
}
foreach (XmlSchemaObject schemaObject in xmlSchema.SchemaTypes.Values)
{
XmlSchemaComplexType schemaComplexType = schemaObject as XmlSchemaComplexType;
if (schemaComplexType != null)
{
ProcessComplexType(schemaComplexType, codeNamespace);
}
}
foreach (XmlSchemaObject schemaObject in xmlSchema.Elements.Values)
{
XmlSchemaElement schemaElement = schemaObject as XmlSchemaElement;
if (schemaElement != null)
{
ProcessElement(schemaElement, codeNamespace);
}
}
return codeCompileUnit;
}
示例15: Execute
/// <summary>
/// Executes the code generator. Called from either the command line exe or the VS.NET wizard. Only public function.
/// </summary>
/// <param name="xsdFile">Path to the XSD file stored on disk</param>
/// <param name="language">Language for generated code</param>
/// <param name="genNamespace">.NET namespace containing the generated types</param>
/// <param name="fileName">File to be generated. If null, namespace name is used</param>
/// <param name="outputLocation">Location for the generated output file</param>
/// <param name="constructRequiredSchema">Build a schema compliancy function -- MakeSchemaCompliant -- into each class</param>
/// <param name="depthFirstTraversalHooks">Add DepthFirstTraversal hooks to fire custom DepthFirstTraversal events on each generated class</param>
/// <param name="defaultInitialization">Set schema default values in class constructors</param>
/// <param name="separateImportedNamespaces">Searate out imported namespaces into their own source files. Default is all types in one file.</param>
/// <param name="namespaceTable">Namespace map table. Null if no imported namespaces exist</param>
/// <param name="filenameTable">Filenames matching the namespaceTable. Null if no imported namespaces exist -- and optional.</param>
/// <param name="partialKeyword">put the .NET 2.0 partial keyword on every class</param>
/// <param name="optionEElements"></param>
/// <param name="acordLookupCodesFile">ACORD lookup codes file -- public</param>
/// <param name="acordLookupCodesFilePrivate">ACORD lookup codes file -- private</param>
/// <returns>result string</returns>
public string[] Execute(string xsdFile, Language language, string genNamespace, string fileName,
string outputLocation, bool constructRequiredSchema, bool depthFirstTraversalHooks, bool defaultInitialization,
ref Hashtable namespaceTable, Hashtable filenameTable, bool partialKeyword, List<string> optionEElements, string acordLookupCodesFile, string acordLookupCodesFilePrivate)
{
FileStream schemaFile = null;
try
{
schemaFile = new FileStream(xsdFile, FileMode.Open, FileAccess.Read);
if (schemaFile == null) throw new XSDObjectGenException("Could not open the XSD schema file: " + xsdFile);
schema = XmlSchema.Read(schemaFile, new ValidationEventHandler(ShowCompileError));
schema.Compile(new ValidationEventHandler(ShowCompileError));
elementFormDefault = schema.ElementFormDefault;
attributeFormDefault = schema.AttributeFormDefault;
if (language == Language.VB)
{
code = (LanguageBase)new VBTemplate();
}
else if (language == Language.CS)
{
code = (LanguageBase)new CSharpTemplate();
}
else
{
throw new XSDObjectGenException(string.Format("Language {0} not supported.", language.ToString()));
}
if (!string.IsNullOrEmpty(acordLookupCodesFile))
{
acordLookup = new XmlDocument();
try
{
acordLookup.Load(acordLookupCodesFile);
}
catch (Exception)
{
throw new XSDObjectGenException("Bad ACORD lookup file path or file");
}
}
if (!string.IsNullOrEmpty(acordLookupCodesFilePrivate))
{
acordLookupPrivate = new XmlDocument();
try
{
acordLookupPrivate.Load(acordLookupCodesFilePrivate);
}
catch (Exception)
{
throw new XSDObjectGenException("Bad ACORD private codes lookup file path or file");
}
}
optionDefaultInitialization = defaultInitialization;
optionConstructRequiredSchema = constructRequiredSchema;
optionDepthFirstTraversalHooks = depthFirstTraversalHooks;
LanguageBase.partialClasses = partialKeyword;
globalComplexTypeClasses = new Hashtable();
globalQualifiedComplexTypeClasses = new Hashtable();
globalQualifiedAbstractTypeClasses = new Hashtable();
enumerations = new Hashtable();
enumerableClasses = new Hashtable();
classesReferencingAbstractTypes = new Hashtable();
ArrayList parentClassStack = new ArrayList(); // arraylist to collect a stack of parent owner .NET classes for nested complexTypes
Globals.globalSchemaTypeTable.Clear(); // clear globals schema type table as it's a global object created at assembly load scope
Globals.globalSeparateImportedNamespaces = false; // means we have imported namespace and will have multiple source files
Globals.globalClrNamespaceList.Clear();
// Get list of all imported schemas -- to be used only if separateImportedNamespaces = true
// Add the main xsd namespace. If separateImportedNamespaces is false, there will be only
// one .net file generated with all of the types from all the xsd namespaces. Otherwise multiple files.
// First namespace in the list is the namespace for the base leaf schema -- for which code is being generated.
ArrayList references = new ArrayList();
if (namespaceTable != null)
{
if (schema.TargetNamespace != null)
genNamespace = (string)namespaceTable[schema.TargetNamespace]; // in case it was changed while changing imported ns names
else
//.........这里部分代码省略.........