コード例 #1
0
ファイル: WsdlExporter.cs プロジェクト: Skycweb/mono-skycweb
        //FIXME: Replace with a dictionary ?
        void AddImport(XmlSchema schema, string ns)
        {
            if (ns == XmlSchema.Namespace || schema.TargetNamespace == ns)
            {
                return;
            }

            foreach (XmlSchemaObject o in schema.Includes)
            {
                XmlSchemaImport import = o as XmlSchemaImport;
                if (import == null)
                {
                    continue;
                }
                if (import.Namespace == ns)
                {
                    return;
                }
            }

            if (ns == string.Empty)
            {
                return;
            }

            XmlSchemaImport imp = new XmlSchemaImport();

            imp.Namespace = ns;
            schema.Includes.Add(imp);
        }
コード例 #2
0
ファイル: SchemaHelper.cs プロジェクト: Potapy4/dotnet-wcf
        static internal void AddImportToSchema(string ns, XmlSchema schema)
        {
            if (NamespacesEqual(ns, schema.TargetNamespace) ||
                NamespacesEqual(ns, XmlSchema.Namespace) ||
                NamespacesEqual(ns, XmlSchema.InstanceNamespace))
            {
                return;
            }

            foreach (object item in schema.Includes)
            {
                if (item is XmlSchemaImport && NamespacesEqual(ns, ((XmlSchemaImport)item).Namespace))
                {
                    return;
                }
            }

            XmlSchemaImport import = new XmlSchemaImport();

            if (ns != null && ns.Length > 0)
            {
                import.Namespace = ns;
            }
            schema.Includes.Add(import);
        }
コード例 #3
0
ファイル: XsdBuilder.cs プロジェクト: hardin253874/Platform
        /// <summary>
        /// Creates a new schema object, and adds various headers.
        /// </summary>
        /// <param name="schemaFile">The schema info object. (Just used so it doesn't add a reference to itself).</param>
        /// <returns>The new schema object.</returns>
        private XmlSchema CreateSchema(SchemaFile schemaFile)
        {
            // Create schema object
            var xsd = new XmlSchema();

            // Set up namespace
            xsd.TargetNamespace = schemaFile.Namespace;
            xsd.Namespaces.Add("", schemaFile.Namespace);
            xsd.ElementFormDefault = XmlSchemaForm.Qualified;

            // Add <imports> and namespaces from schemas to all schemas
            // (this could be improved so that only referenced schemas are imported)
            foreach (SchemaFile foreignSchema in _namespaceToSchema.Values)
            {
                // Don't import self
                if (foreignSchema == schemaFile)
                {
                    continue;
                }

                // Add namespace
                string nsAlias = foreignSchema.Namespace;   // use the namespace as its own XML alias. Ok, while aliases are nice and simple.
                xsd.Namespaces.Add(nsAlias, foreignSchema.Namespace);

                // Add import
                XmlSchemaImport import = new XmlSchemaImport();
                import.Namespace      = foreignSchema.Namespace;
                import.SchemaLocation = RelativePath(foreignSchema.Path, schemaFile.Path);   // todo: calculate relative path of one schema to another
                xsd.Includes.Add(import);
            }
            return(xsd);
        }
コード例 #4
0
        internal void AddExternal(XmlSchema schema, string ns, string location)
        {
            if (schema == null)
            {
                return;
            }

            if (schema.TargetNamespace == ns)
            {
                XmlSchemaInclude include = new XmlSchemaInclude();
                include.SchemaLocation = location;
                this.AddUriFixup(delegate(Uri current)
                {
                    include.SchemaLocation = CombineUris(current, include.SchemaLocation);
                });
                schema.Includes.Add(include);
            }
            else
            {
                XmlSchemaImport import = new XmlSchemaImport();
                import.SchemaLocation = location;
                this.AddUriFixup(delegate(Uri current)
                {
                    import.SchemaLocation = CombineUris(current, import.SchemaLocation);
                });
                import.Namespace = ns;
                schema.Includes.Add(import);
            }
        }
コード例 #5
0
        internal void ReflectStringParametersMessage()
        {
            Message inputMessage = InputMessage;

            foreach (ParameterInfo parameterInfo in Method.InParameters)
            {
                MessagePart part = new MessagePart();
                part.Name = XmlConvert.EncodeLocalName(parameterInfo.Name);
                if (parameterInfo.ParameterType.IsArray)
                {
                    string typeNs = DefaultNamespace;
                    if (typeNs.EndsWith("/", StringComparison.Ordinal))
                    {
                        typeNs += "AbstractTypes";
                    }
                    else
                    {
                        typeNs += "/AbstractTypes";
                    }
                    string typeName = "StringArray";
                    if (!ServiceDescription.Types.Schemas.Contains(typeNs))
                    {
                        XmlSchema schema = new XmlSchema();
                        schema.TargetNamespace = typeNs;
                        ServiceDescription.Types.Schemas.Add(schema);

                        XmlSchemaElement element = new XmlSchemaElement();
                        element.Name           = "String";
                        element.SchemaTypeName = new XmlQualifiedName("string", XmlSchema.Namespace);
                        element.MinOccurs      = decimal.Zero;
                        element.MaxOccurs      = decimal.MaxValue;
                        XmlSchemaSequence all = new XmlSchemaSequence();
                        all.Items.Add(element);

                        XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();
                        restriction.BaseTypeName = new XmlQualifiedName(Soap.ArrayType, Soap.Encoding);
                        restriction.Particle     = all;

                        XmlSchemaImport import = new XmlSchemaImport();
                        import.Namespace = restriction.BaseTypeName.Namespace;

                        XmlSchemaComplexContent model = new XmlSchemaComplexContent();
                        model.Content = restriction;

                        XmlSchemaComplexType type = new XmlSchemaComplexType();
                        type.Name         = typeName;
                        type.ContentModel = model;

                        schema.Items.Add(type);
                        schema.Includes.Add(import);
                    }
                    part.Type = new XmlQualifiedName(typeName, typeNs);
                }
                else
                {
                    part.Type = new XmlQualifiedName("string", XmlSchema.Namespace);
                }
                inputMessage.Parts.Add(part);
            }
        }
コード例 #6
0
        static void AddImport(XmlSchema schema, string importNamespace)
        {
            XmlSchemaImport importElement = new XmlSchemaImport();

            importElement.Namespace = importNamespace;
            schema.Includes.Add(importElement);
        }
コード例 #7
0
ファイル: SchemaHelper.cs プロジェクト: zpk513/corefx
        internal static void AddSchemaImport(string ns, XmlSchema schema)
        {
            if (SchemaHelper.NamespacesEqual(ns, schema.TargetNamespace) || SchemaHelper.NamespacesEqual(ns, Globals.SchemaNamespace) || SchemaHelper.NamespacesEqual(ns, Globals.SchemaInstanceNamespace))
            {
                return;
            }

            foreach (object item in schema.Includes)
            {
                if (item is XmlSchemaImport)
                {
                    if (SchemaHelper.NamespacesEqual(ns, ((XmlSchemaImport)item).Namespace))
                    {
                        return;
                    }
                }
            }

            XmlSchemaImport import = new XmlSchemaImport();

            if (ns != null && ns.Length > 0)
            {
                import.Namespace = ns;
            }
            schema.Includes.Add(import);
        }
コード例 #8
0
ファイル: xsd.cs プロジェクト: ydunk/masters
        static void AddFakeSchemas(XmlSchema parent, XmlSchemas schemas)
        {
            if (schemas[XmlSchema.Namespace] == null)
            {
                XmlSchemaImport import = new XmlSchemaImport();
                import.Namespace = XmlSchema.Namespace;
                import.Schema    = CreateFakeXsdSchema(XmlSchema.Namespace, "schema");
                parent.Includes.Add(import);
            }

            if (schemas[Namespace.SoapEncoding] == null)
            {
                XmlSchemaImport import = new XmlSchemaImport();
                import.Namespace = Namespace.SoapEncoding;
                import.Schema    = CreateFakeSoapEncodingSchema(Namespace.SoapEncoding, "Array");
                parent.Includes.Add(import);
            }

            if (schemas[Namespace.Wsdl] == null)
            {
                XmlSchemaImport import = new XmlSchemaImport();
                import.Namespace = Namespace.Wsdl;
                import.Schema    = CreateFakeWsdlSchema(Namespace.Wsdl);
                parent.Includes.Add(import);
            }
        }
コード例 #9
0
    public static void Main()
    {


        XmlSchema schema = new XmlSchema();
        schema.ElementFormDefault = XmlSchemaForm.Qualified;
        schema.TargetNamespace = "http://www.w3.org/2001/05/XMLInfoset";

        // <xs:import namespace="http://www.example.com/IPO" />                            
        XmlSchemaImport import = new XmlSchemaImport();
        import.Namespace = "http://www.example.com/IPO";
        schema.Includes.Add(import);

        // <xs:include schemaLocation="example.xsd" />               
        XmlSchemaInclude include = new XmlSchemaInclude();
        include.SchemaLocation = "example.xsd";
        schema.Includes.Add(include);

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
        schemaSet.Add(schema);
        schemaSet.Compile();

        XmlSchema compiledSchema = null;

        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema.Write(Console.Out, nsmgr);

    }/* Main() */
コード例 #10
0
ファイル: MetsDocument.cs プロジェクト: Plankankul/Zenity
        /// <summary>
        /// Load Dublin Core xml schema from resource files.
        /// </summary>
        private void LoadDcSchema()
        {
            XmlTextReader simpleDCSchemaReader   = null;
            XmlTextReader dublinCoreSchemaReader = null;
            XmlTextReader xmlSchemaReader        = null;

            try
            {
                // Load Schema which describes "mets:xmlData" can contain "dc:elementContainer" types.
                simpleDCSchemaReader = new XmlTextReader(
                    Resources.SimpleDcSchema,
                    XmlNodeType.Document,
                    null);
                DublinCoreSchema = XmlSchema.Read(simpleDCSchemaReader, null);

                // Load Dublin Core xml Schema with "dc:elementContainer" types at namespace "http://purl.org/dc/elements/1.1/" .
                dublinCoreSchemaReader = new XmlTextReader(
                    Resources.DcSchema,
                    XmlNodeType.Document,
                    null);
                XmlSchema dublinCoreSchema = XmlSchema.Read(dublinCoreSchemaReader, null);

                // Load xml schema for namespace "http://www.w3.org/XML/1998/namespace" which is imported in Dublin Core schema.
                xmlSchemaReader = new XmlTextReader(
                    Resources.XmlSchema,
                    XmlNodeType.Document,
                    null);
                XmlSchema xmlSchema = XmlSchema.Read(xmlSchemaReader, null);


                // Import "http://www.w3.org/XML/1998/namespace" namespace in Dublin Core xml schema.
                XmlSchemaImport dublinCoreInclude = new XmlSchemaImport();
                dublinCoreInclude.Namespace = MetsConstants.XmlNamespace;
                dublinCoreInclude.Schema    = xmlSchema;
                dublinCoreSchema.Includes.Add(dublinCoreInclude);

                // Import "http://purl.org/dc/elements/1.1/" namespace in simple Dublin Core xml schema.
                XmlSchemaImport simpleDcInclude = new XmlSchemaImport();
                simpleDcInclude.Namespace = MetsConstants.DublinCoreNamespace;
                simpleDcInclude.Schema    = dublinCoreSchema;
                DublinCoreSchema.Includes.Add(simpleDcInclude);
            }
            finally
            {
                if (null != simpleDCSchemaReader)
                {
                    simpleDCSchemaReader.Close();
                }
                if (null != dublinCoreSchemaReader)
                {
                    dublinCoreSchemaReader.Close();
                }
                if (null != xmlSchemaReader)
                {
                    xmlSchemaReader.Close();
                }
            }
        }
コード例 #11
0
        private static void ConstructSchemaImports(InterfaceContract serviceInterfaceContract, bool isRoundTrip, System.Web.Services.Description.ServiceDescription desc)
        {
            XmlSchema typesSchema = null;

            // Are we round-tripping? Then we have to access the existing types
            // section.
            // Otherwise we just initialize a new XmlSchema for types.
            if (isRoundTrip)
            {
                typesSchema = desc.Types.Schemas[desc.TargetNamespace];
                // if we don't have a types section belonging to the same namespace as service description
                // we take the first types section available.
                if (typesSchema == null)
                {
                    typesSchema = desc.Types.Schemas[0];
                }
                // Remove the includes. We gonna re-add them later in this operation.
                typesSchema.Includes.Clear();
            }
            else
            {
                typesSchema = new XmlSchema();
            }

            // Add imports to the types section resolved above.
            foreach (SchemaImport import in serviceInterfaceContract.Imports)
            {
                XmlSchemaExternal importedSchema = null;
                if (import.SchemaNamespace == null || import.SchemaNamespace == "")
                {
                    importedSchema = new XmlSchemaInclude();
                }
                else
                {
                    importedSchema = new XmlSchemaImport();
                    ((XmlSchemaImport)importedSchema).Namespace = import.SchemaNamespace;
                }
                if (serviceInterfaceContract.UseAlternateLocationForImports)
                {
                    importedSchema.SchemaLocation = import.AlternateLocation;
                }
                else
                {
                    importedSchema.SchemaLocation = import.SchemaLocation;
                }
                typesSchema.Includes.Add(importedSchema);
            }

            // If we are not round-tripping we have to link the types schema we just created to
            // the service description.
            if (!isRoundTrip)
            {
                // Finally add the type schema to the ServiceDescription.Types.Schemas collection.
                desc.Types.Schemas.Add(typesSchema);
            }
        }
コード例 #12
0
        public override void Check(ConformanceCheckContext ctx, XmlSchemaImport value)
        {
            // LAMESPEC: same here to Check() for Import.
            XmlSchema doc = ctx.GetDocument(value.SchemaLocation, value.Namespace) as XmlSchema;

            if (doc == null)
            {
                ctx.ReportError(value, "Schema '" + value.SchemaLocation + "' not found");
            }
        }
コード例 #13
0
        public void Write(XmlWriter writer)
        {
            var schema = new XmlSchema();

            if (this.dataSet != null)
            {
                this.WriteSchemaAttribute(schema, this.dataSet.Namespace);
                this.WriteGuidType(schema);
                this.WriteDataTypes(schema, dataSet.Types.OrderBy(item => item.Name));
                this.WriteTables(schema, dataSet.DataSetName, dataSet.Tables);
            }
            else if (this.dataTable != null)
            {
                var contentName    = this.dataTable.DataSet != null ? this.dataTable.DataSet.DataSetName : CremaDataSet.DefaultDataSetName;
                var tableNamespace = this.itemName == null ? this.dataTable.Namespace : CremaSchema.TableNamespace + this.itemName.CategoryPath + this.itemName.Name;
                this.WriteSchemaAttribute(schema, tableNamespace);
                this.WriteGuidType(schema);

                var tables = this.IsRecursive == true?EnumerableUtility.FamilyTree(dataTable, item => item.Childs) : Enumerable.Repeat(dataTable, 1);

                if (this.dataTable.DataSet != null)
                {
                    var columns = tables.SelectMany(item => item.Columns).Distinct();
                    var query   = from item in this.dataTable.DataSet.Types
                                  join column in columns on item.Path equals column.DataTypeName
                                  select item;

                    var index = 0;
                    foreach (var item in query.Distinct())
                    {
                        var typeSchema = new XmlSchema();
                        WriteSchemaAttribute(typeSchema, item.Namespace);
                        WriteDataType(typeSchema, item);

                        var import = new XmlSchemaImport()
                        {
                            Schema         = typeSchema,
                            Namespace      = item.Namespace,
                            SchemaLocation = UriUtility.MakeRelative(tableNamespace, item.Namespace) + CremaSchema.SchemaExtension
                        };
                        schema.Includes.Add(import);
                        index++;
                    }
                }

                this.WriteTables(schema, contentName, tables);
            }
            else if (this.dataType != null)
            {
                this.WriteSchemaAttribute(schema, this.dataType.Namespace);
                this.WriteDataTypes(schema, new CremaDataType[] { this.dataType });
            }

            schema.Write(writer);
        }
コード例 #14
0
        public static void FixBaseDocumentInheritance(XmlSchemaSet schemaSet)
        {
            var schemas        = schemaSet.Schemas().Cast <XmlSchema>().ToList();
            var maindocSchemas = schemas.Where(x => x.SourceUri.Contains("maindoc")).ToList();
            var baseDocSchema  = schemas.Single(x => x.SourceUri.Contains("BaseDocument"));

            var elementsToRemove = (baseDocSchema.Items.OfType <XmlSchemaComplexType>().Single().ContentTypeParticle as XmlSchemaSequence)
                                   ?.Items.Cast <XmlSchemaElement>()
                                   .ToLookup(x => x.QualifiedName.Name) ?? new XmlSchemaElement[0].ToLookup(x => "");

            var baseDocSchemaImport = new XmlSchemaImport {
                Namespace = baseDocSchema.TargetNamespace, Schema = baseDocSchema
            };

            foreach (var maindocSchema in maindocSchemas)
            {
                if (maindocSchema.SourceUri.Contains("BaseDocument"))
                {
                    continue;
                }

                maindocSchema.Namespaces.Add("abs", baseDocSchema.TargetNamespace);
                maindocSchema.Includes.Add(baseDocSchemaImport);

                var maindocSchemaComplexType = maindocSchema.Items.OfType <XmlSchemaComplexType>().Single();
                var sequence = (XmlSchemaSequence)maindocSchemaComplexType.Particle;
                var removed  = 0;
                for (var i = sequence.Items.Count - 1; i >= 0; i--)
                {
                    var el = (XmlSchemaElement)sequence.Items[i];
                    if (elementsToRemove.Contains(el.QualifiedName.Name))
                    {
                        sequence.Items.RemoveAt(i);
                        removed++;
                    }
                }

                if (removed != elementsToRemove.Count)
                {
                    throw new InvalidOperationException("Invalid base document, not all maindocs have all the base document properties.");
                }

                maindocSchemaComplexType.ContentModel = new XmlSchemaComplexContent()
                {
                    Content = new XmlSchemaComplexContentExtension
                    {
                        BaseTypeName = new XmlQualifiedName(baseDocSchema.Items.OfType <XmlSchemaComplexType>().Single().Name, baseDocSchema.TargetNamespace),
                        Particle     = sequence
                    }
                };
            }

            maindocSchemas.ForEach(s => schemaSet.Reprocess(s));
        }
コード例 #15
0
ファイル: CremaSchemaWriter.cs プロジェクト: teize001/Crema
        public void Write(XmlWriter writer)
        {
            var schema = new XmlSchema();

            if (this.dataSet != null)
            {
                this.WriteSchemaAttribute(schema, this.dataSet.Namespace);
                this.WriteGuidType(schema);
                this.WriteDataTypes(schema, dataSet.Types);
                this.WriteTables(schema, dataSet.DataSetName, dataSet.Tables.Where(item => item.Parent == null).OrderBy(item => item.TemplateNamespace));
            }
            else if (this.dataTable != null)
            {
                var contentName    = this.dataTable.DataSet != null ? this.dataTable.DataSet.DataSetName : CremaDataSet.DefaultDataSetName;
                var tableNamespace = this.itemName == null ? this.dataTable.Namespace : CremaSchema.TableNamespace + this.itemName.CategoryPath + this.itemName.Name;
                this.WriteSchemaAttribute(schema, tableNamespace);
                this.WriteGuidType(schema);

                if (this.dataTable.DataSet != null)
                {
                    var query = from item in this.dataTable.DataSet.Types
                                join c in this.dataTable.Columns on item.TypeName equals c.DataType.GetTypeName()
                                select item;

                    int index = 0;
                    foreach (var item in query.Distinct())
                    {
                        var typeSchema = new XmlSchema();
                        WriteSchemaAttribute(typeSchema, item.Namespace);
                        WriteDataType(typeSchema, item);

                        var import = new XmlSchemaImport()
                        {
                            Schema         = typeSchema,
                            Namespace      = item.Namespace,
                            SchemaLocation = UriUtility.MakeRelative(this.dataTable.Namespace, item.Namespace) + CremaSchema.SchemaExtension
                        };
                        schema.Includes.Add(import);
                        schema.Namespaces.Add("d" + index, item.Namespace);
                        index++;
                    }
                }

                this.WriteTables(schema, contentName, new CremaDataTable[] { this.dataTable });
            }
            else if (this.dataType != null)
            {
                this.WriteSchemaAttribute(schema, this.dataType.Namespace);
                this.WriteDataTypes(schema, new CremaDataType[] { this.dataType });
            }

            schema.Write(writer);
        }
コード例 #16
0
        private void AddExternalSchemas(XmlSchemaObjectCollection includes)
        {
            /// @todo handle imports in an extendable manner (via ExtPoint).
            XmlSchema xmlSchema =
                XmlUtils.GetSchema(_assemblyLoaderService.LoadAssembly("jingxian.core.runtime"), "jingxian.core.runtime.Schemas.xml.xsd");
            XmlSchemaImport xmlImport = new XmlSchemaImport();

            xmlImport.Schema    = xmlSchema;
            xmlImport.Namespace = xmlSchema.TargetNamespace;

            includes.Add(xmlImport);
        }
コード例 #17
0
ファイル: ValidateMisc.cs プロジェクト: zhongjn/corefx
        public void AddEditInvalidImport(string testDir, string testFile, int expCountGT, int expCountGE)
        {
            string xsd = Path.Combine(path, testDir, testFile);

            XmlSchemaSet ss      = new XmlSchemaSet();
            XmlSchema    Schema  = XmlSchema.Read(XmlReader.Create(xsd), null);
            XmlSchema    Schema1 = ss.Add(Schema);

            ValidateSchemaSet(ss, 1, false, 0, 0, 0, "Validation after add");

            ss.Compile();
            ValidateSchemaSet(ss, 1, true, expCountGT, expCountGE, 0, "Validation after add/comp");

            XmlSchemaImport imp = new XmlSchemaImport();

            imp.Namespace      = "ns-a";
            imp.SchemaLocation = "reprocess_v9_a.xsd";
            Schema.Includes.Add(imp);

            try
            {
                ss.Reprocess(Schema);
                Assert.True(false);
            }
            catch (XmlSchemaException e)
            {
                _output.WriteLine(e.Message);
            }
            ValidateSchemaSet(ss, 1, false, 1, 0, 0, "Validation after repr");

            try
            {
                ss.Compile();
                Assert.True(false);
            }
            catch (XmlSchemaException e)
            {
                _output.WriteLine(e.Message);
            }
            ValidateSchemaSet(ss, 1, false, 1, 0, 0, "Validation after repr/comp");

            try
            {
                ValidateWithSchemaInfo(ss);
                Assert.True(false);
            }
            catch (XmlSchemaValidationException e)
            {
                _output.WriteLine(e.Message);
            }
            return;
        }
コード例 #18
0
        private static void AddImports(XmlSchema schema, GeneratorContext context, IDocLibrary docLibrary)
        {
            if (context.Annotate)
            {
                var import = new XmlSchemaImport
                {
                    Namespace      = "urn:un:unece:uncefact:documentation:standard:XMLNDRDocumentation:3",
                    SchemaLocation = "documentation/standard/XMLNDR_Documentation_3p0.xsd"
                };

                schema.Includes.Add(import);
            }
        }
コード例 #19
0
    static void Main(string[] args)
    {
        // Add the customer and address schemas to a new XmlSchemaSet and compile them.
        // Any schema validation warnings and errors encountered reading or
        // compiling the schemas are handled by the ValidationEventHandler delegate.
        XmlSchemaSet schemaSet = new XmlSchemaSet();

        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
        schemaSet.Add("http://www.tempuri.org", "customer.xsd");
        schemaSet.Add("http://www.example.com/IPO", "address.xsd");
        schemaSet.Compile();

        // Retrieve the compiled XmlSchema objects for the customer and
        // address schema from the XmlSchemaSet by iterating over
        // the Schemas property.
        XmlSchema customerSchema = null;
        XmlSchema addressSchema  = null;

        foreach (XmlSchema schema in schemaSet.Schemas())
        {
            if (schema.TargetNamespace == "http://www.tempuri.org")
            {
                customerSchema = schema;
            }
            else if (schema.TargetNamespace == "http://www.example.com/IPO")
            {
                addressSchema = schema;
            }
        }

        // Create an XmlSchemaImport object, set the Namespace property
        // to the namespace of the address schema, the Schema property
        // to the address schema, and add it to the Includes property
        // of the customer schema.
        XmlSchemaImport import = new XmlSchemaImport();

        import.Namespace = "http://www.example.com/IPO";
        import.Schema    = addressSchema;
        customerSchema.Includes.Add(import);

        // Reprocess and compile the modified XmlSchema object
        // of the customer schema and write it to the console.
        schemaSet.Reprocess(customerSchema);
        schemaSet.Compile();
        customerSchema.Write(Console.Out);

        // Recursively write all of the schemas imported into the
        // customer schema to the console using the Includes
        // property of the customer schema.
        RecurseExternals(customerSchema);
    }
コード例 #20
0
ファイル: xsd.cs プロジェクト: ydunk/masters
        private static void Compile(XmlSchemas userSchemas)
        {
            XmlSchema parent = new XmlSchema();

            foreach (XmlSchema s in userSchemas)
            {
                if (s.TargetNamespace != null && s.TargetNamespace.Length == 0)
                {
                    s.TargetNamespace = null;
                }

                if (s.TargetNamespace == parent.TargetNamespace)
                {
                    XmlSchemaInclude include = new XmlSchemaInclude();
                    include.Schema = s;
                    parent.Includes.Add(include);
                }
                else
                {
                    XmlSchemaImport import = new XmlSchemaImport();
                    import.Namespace = s.TargetNamespace;
                    import.Schema    = s;
                    parent.Includes.Add(import);
                }
            }

            /*  to be able to compile multiple schemas using DataSets and encoded Arrays we need three additional schemas :
             *       XmlSchema:     <xs:schema targetNamespace="http://www.w3.org/2001/XMLSchema"..
             *       SoapEncoding:  <xs:schema targetNamespace="http://schemas.xmlsoap.org/soap/encoding/"..
             *       Wsdl:          <xs:schema targetNamespace="http://schemas.xmlsoap.org/wsdl/"..
             *
             *  we do not need them for our validation, just to fool XmlSchema.Compile, so we are going to create the bare minimum schemas
             */

            AddFakeSchemas(parent, userSchemas);

            try {
                XmlSchemaCollection xsc = new XmlSchemaCollection();
                xsc.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackWithErrorCode);
                xsc.Add(parent);

                if (xsc.Count == 0)
                {
                    Console.WriteLine(Environment.NewLine + Res.GetString(Res.SchemaValidationWarning) + Environment.NewLine);
                }
            }
            catch (Exception e) {
                Console.WriteLine(Environment.NewLine + Res.GetString(Res.SchemaValidationWarning) + Environment.NewLine + e.Message + Environment.NewLine);
            }
        }
コード例 #21
0
 private XmlSchemaImport FindImport(XmlSchema schema, string ns)
 {
     foreach (object obj2 in schema.Includes)
     {
         if (obj2 is XmlSchemaImport)
         {
             XmlSchemaImport import = (XmlSchemaImport)obj2;
             if (import.Namespace == ns)
             {
                 return(import);
             }
         }
     }
     return(null);
 }
コード例 #22
0
 XmlSchemaImport FindImport(XmlSchema schema, string ns)
 {
     foreach (object item in schema.Includes)
     {
         if (item is XmlSchemaImport)
         {
             XmlSchemaImport import = (XmlSchemaImport)item;
             if (import.Namespace == ns)
             {
                 return(import);
             }
         }
     }
     return(null);
 }
コード例 #23
0
        private string ResolveImportPath(XmlSchemaImport import)
        {
            string xsdImportPath;

            if (!Path.IsPathRooted(import.SchemaLocation))
            {
                xsdImportPath = Path.Combine(Path.GetDirectoryName(this.xsdFile), import.SchemaLocation);
            }
            else
            {
                xsdImportPath = import.SchemaLocation;
            }

            return(xsdImportPath);
        }
コード例 #24
0
        public void TestSimpleImport()
        {
            XmlSchema schema = XmlSchema.Read(new XmlTextReader("Test/XmlFiles/xsd/3.xsd"), null);

            Assert.AreEqual("urn:foo", schema.TargetNamespace);
            XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;

            Assert.IsNotNull(import);

            schema.Compile(null);
            Assert.AreEqual(4, schema.Elements.Count);
            Assert.IsNotNull(schema.Elements [QName("Foo", "urn:foo")]);
            Assert.IsNotNull(schema.Elements [QName("Bar", "urn:foo")]);
            Assert.IsNotNull(schema.Elements [QName("Foo", "urn:bar")]);
            Assert.IsNotNull(schema.Elements [QName("Bar", "urn:bar")]);
        }
コード例 #25
0
        public static void ModifyMaindocSchemasForInheritance(XmlSchemaSet schemaSet)
        {
            var maindocSchemas = schemaSet.Schemas().Cast <XmlSchema>().Where(x => x.SourceUri.Contains("maindoc")).ToList();

            var sharedElementCount = GetSharedElementCount(maindocSchemas);

            if (0 == sharedElementCount)
            {
                throw new InvalidOperationException("Could not find any shared elements.");
            }

            var templateSchema     = maindocSchemas.First();
            var abstractBaseSchema = CreateAbstractBaseSchemaFromMaindocSchema(templateSchema, sharedElementCount);

            var abstactBaseSchemaImport = new XmlSchemaImport {
                Namespace = abstractBaseSchema.TargetNamespace, Schema = abstractBaseSchema
            };
            var abstactBaseSchemaQNameToInheritFrom = new XmlQualifiedName(AbstractBaseSchemaComplexTypeName, abstractBaseSchema.TargetNamespace);

            foreach (var maindocSchema in maindocSchemas)
            {
                maindocSchema.Namespaces.Add("abs", abstractBaseSchema.TargetNamespace);
                maindocSchema.Includes.Add(abstactBaseSchemaImport);

                var maindocSchemaComplexType = maindocSchema.Items.OfType <XmlSchemaComplexType>().Single();
                var nonSharedElementSequence = maindocSchemaComplexType.Particle as XmlSchemaSequence;
                if (nonSharedElementSequence != null)
                {
                    for (var i = 0; i < sharedElementCount; i++)
                    {
                        nonSharedElementSequence.Items.RemoveAt(0);
                    }
                }

                maindocSchemaComplexType.ContentModel = new XmlSchemaComplexContent
                {
                    Content = new XmlSchemaComplexContentExtension
                    {
                        BaseTypeName = abstactBaseSchemaQNameToInheritFrom,
                        Particle     = nonSharedElementSequence
                    }
                };
            }

            schemaSet.Add(abstractBaseSchema);
            maindocSchemas.ForEach(s => schemaSet.Reprocess(s));
        }
コード例 #26
0
 bool ImportsEncodedNamespace(XmlSchema sc)
 {
     foreach (XmlSchemaObject ob in sc.Includes)
     {
         XmlSchemaImport import = ob as XmlSchemaImport;
         if (import == null)
         {
             continue;
         }
         if (import.Namespace == Soap11BindingExtensionReflector.EncodingNamespace ||
             import.Namespace == Soap12BindingExtensionReflector.EncodingNamespace)
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #27
0
        /// <summary>
        /// Find common elements amongst all maindocSchemas and put them in a new abstractBaseSchema.
        /// Modify maindocsSchemas to inherit from abstractBaseSchema and remove elements that now go
        /// into abstractBaseSchema.
        /// </summary>
        /// <param name="maindocSchemas"></param>
        /// <returns>Base schema</returns>
        public static XmlSchema ModifyMaindocSchemasForInheritance(ICollection <XmlSchema> maindocSchemas)
        {
            int sharedElementCount = GetSharedElementCount(maindocSchemas);

            if (0 == sharedElementCount)
            {
                throw new ApplicationException("Maindoc schemas do not seem to have any shared elements. Inheritance from a baseclass pointless."
                                               + "Have you mixed-up xsdfiles in common and maindoc folders?");
            }

            // Construct new abstract base from an arbitrary maindoc schema (use it as template). Just pick the first one and pray
            XmlSchema templateSchema     = maindocSchemas.First();
            XmlSchema abstractBaseSchema = CreateAbstractBaseSchemaFromMaindocSchema(templateSchema, sharedElementCount);

            XmlSchemaImport abstactBaseSchemaImport = new XmlSchemaImport {
                Namespace = abstractBaseSchema.TargetNamespace, Schema = abstractBaseSchema
            };
            XmlQualifiedName abstactBaseSchemaQNameToInheritFrom = new XmlQualifiedName(Constants.abstractBaseSchemaComplexTypeName, abstractBaseSchema.TargetNamespace);

            foreach (XmlSchema maindocSchema in maindocSchemas)
            {
                maindocSchema.Namespaces.Add("abs", abstractBaseSchema.TargetNamespace);
                maindocSchema.Includes.Add(abstactBaseSchemaImport);

                XmlSchemaComplexType maindocSchemaComplexType = maindocSchema.Items.OfType <XmlSchemaComplexType>().Single(); // Single is safe. Should only be one.
                XmlSchemaSequence    nonSharedElementSequence = maindocSchemaComplexType.Particle as XmlSchemaSequence;
                //maindocSchemaComplexType.Particle = null; // do I have to do this? Has no effect

                // remove shared elements (they are now in the base we inherit from)
                for (int i = 0; i < sharedElementCount; i++)
                {
                    nonSharedElementSequence.Items.RemoveAt(0);
                }

                maindocSchemaComplexType.ContentModel = new XmlSchemaComplexContent
                {
                    Content = new XmlSchemaComplexContentExtension
                    {
                        BaseTypeName = abstactBaseSchemaQNameToInheritFrom,
                        Particle     = nonSharedElementSequence
                    }
                };
            }

            return(abstractBaseSchema);
        }
コード例 #28
0
 void AddIncludingSchema(XmlSchemas list, string ns)
 {
     foreach (XmlSchema sc in Schemas)
     {
         if (sc.TargetNamespace == ns && !list.Contains(sc))
         {
             list.Add(sc);
             foreach (XmlSchemaObject ob in sc.Includes)
             {
                 XmlSchemaImport import = ob as XmlSchemaImport;
                 if (import != null)
                 {
                     AddIncludingSchema(list, import.Namespace);
                 }
             }
         }
     }
 }
コード例 #29
0
 private void AddImport(XmlSchema schema, Hashtable imports)
 {
     if ((schema != null) && (imports[schema] == null))
     {
         imports.Add(schema, schema);
         foreach (XmlSchemaExternal external in schema.Includes)
         {
             if (external is XmlSchemaImport)
             {
                 XmlSchemaImport import = (XmlSchemaImport)external;
                 foreach (XmlSchema schema2 in this.allSchemas.GetSchemas(import.Namespace))
                 {
                     this.AddImport(schema2, imports);
                 }
             }
         }
     }
 }
コード例 #30
0
 private void AddSchemaImport(string ns, string referencingNs)
 {
     if (((referencingNs != null) && (ns != null)) && (ns != referencingNs))
     {
         XmlSchema schema = this.schemas[referencingNs];
         if (schema == null)
         {
             throw new InvalidOperationException(Res.GetString("XmlMissingSchema", new object[] { referencingNs }));
         }
         if (((ns != null) && (ns.Length > 0)) && (this.FindImport(schema, ns) == null))
         {
             XmlSchemaImport item = new XmlSchemaImport {
                 Namespace = ns
             };
             schema.Includes.Add(item);
         }
     }
 }