Пример #1
0
        private CodeCompileUnit GenerateTypesWithDataContractImporter(XmlSchemaSet schemas)
        {
            XsdDataContractImporter importer = new XsdDataContractImporter();

            importer.Options = CreateImportOptions(false);

            schemas.ValidationEventHandler += this.validationEventHandler;

            try
            {
                importer.Import(schemas);
                return(importer.CodeCompileUnit);
            }
            catch (InvalidDataContractException dataContractException)
            {
                // double check since CanImport may trigger a "false" positive
                // after adding serialization and types schemas
                importer.CanImport(schemas);
                if (!importer.CanImport(schemas))
                {
                    throw new InvalidSerializerException(dataContractException.Message);
                }
                throw;
            }
            catch (ArgumentException argumentException)
            {
                throw new InvalidOperationException(argumentException.Message, argumentException.InnerException);
            }
            finally
            {
                schemas.ValidationEventHandler -= this.validationEventHandler;
            }
        }
Пример #2
0
        MessagePartDescription CreateMessagePart(XmlSchemaElement elem, Message msg, MessagePart msgPart)
        {
            var part = new MessagePartDescription(elem.QualifiedName.Name, elem.QualifiedName.Namespace);

            part.DataContractImporter = dc_importer;
            if (dc_importer.CanImport(schema_set_in_use, elem))
            {
                var typeQName = dc_importer.Import(schema_set_in_use, elem);
                part.CodeTypeReference = dc_importer.GetCodeTypeReference(elem.ElementSchemaType.QualifiedName, elem);
            }
            return(part);
        }
		public void CanImportTest2 ()
		{
			NewXmlSchemaSet ();
			XsdDataContractImporter xsdi = GetImporter ();

			List<XmlQualifiedName> names = new List<XmlQualifiedName> ();
			names.Add (new XmlQualifiedName ("Echo", "http://myns/echo"));
			Assert.IsFalse (xsdi.CanImport (xss, names), "#ci20");

			names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
			Assert.IsFalse (xsdi.CanImport (xss, names), "#ci21");

			names.Clear ();
			names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
			Assert.IsTrue (xsdi.CanImport (xss, names), "#ci22");
		}
		[Category ("NotWorking")] // importing almost-invalid element. This test is almost missing the point.
		public void ImportTest ()
		{
			XsdDataContractImporter xsdi = GetImporter ();
			XmlSchemaElement element = new XmlSchemaElement();
			Assert.IsTrue (xsdi.CanImport (xss, new QName ("dc", "http://schemas.datacontract.org/2004/07/")), "#i01");
			Assert.IsTrue (xsdi.CanImport (xss, element), "#i01-2");
			Assert.AreEqual (new XmlQualifiedName ("anyType", XmlSchema.Namespace), xsdi.Import (xss, element), "#i02");

			CodeCompileUnit ccu = xsdi.CodeCompileUnit;
			Assert.AreEqual (1, ccu.Namespaces.Count, "#i03");
			Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i04");

			Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i05");

			Dictionary<string, string> mbrs = new Dictionary<string, string> ();
			mbrs.Add ("foo", "System.String");
			CheckDC (ccu.Namespaces [0].Types [0], "dc", mbrs, "#i06");
		}
        CodeCompileUnit DoImport(bool xmlType, params string [] schemaFiles)
        {
            var ccu = new CodeCompileUnit();
            var xdi = new XsdDataContractImporter(ccu);

            if (xmlType)
            {
                xdi.Options = new ImportOptions()
                {
                    ImportXmlType = true
                }
            }
            ;
            var xss = new XmlSchemaSet();

            foreach (var schemaFile in schemaFiles)
            {
                xss.Add(null, schemaFile);
            }
            xdi.Import(xss);

            return(ccu);
        }

        void DoCanImport(bool result, params string [] schemaFiles)
        {
            var ccu = new CodeCompileUnit();
            var xdi = new XsdDataContractImporter(ccu);
            var xss = new XmlSchemaSet();

            foreach (var schemaFile in schemaFiles)
            {
                xss.Add(null, schemaFile);
            }
            Assert.AreEqual(result, xdi.CanImport(xss));
        }

        string GenerateCode(CodeCompileUnit ccu)
        {
            var sw = new StringWriter();

            new CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, sw, null);
            return(sw.ToString());
        }
        //<snippet2>
        //<snippet3>
        static CodeCompileUnit Import(XmlSchemaSet schemas)
        {
            XsdDataContractImporter imp = new XsdDataContractImporter();

            // The EnableDataBinding option adds a RaisePropertyChanged method to
            // the generated code. The GenerateInternal causes code access to be
            // set to internal.
            ImportOptions iOptions = new ImportOptions();

            iOptions.EnableDataBinding = true;
            iOptions.GenerateInternal  = true;
            imp.Options = iOptions;

            if (imp.CanImport(schemas))
            {
                imp.Import(schemas);
                return(imp.CodeCompileUnit);
            }
            else
            {
                return(null);
            }
        }
        public void CanImportTest()
        {
            NewXmlSchemaSet();
            XsdDataContractImporter xsdi = GetImporter();

            Assert.IsFalse(xss.IsCompiled, "#ci01");

            Assert.IsTrue(xsdi.CanImport(xss, new XmlQualifiedName("dc", "http://schemas.datacontract.org/2004/07/")), "#ci02");
            Assert.IsTrue(xss.IsCompiled, "#ci03");

            Assert.IsFalse(xsdi.CanImport(xss, new XmlQualifiedName("Echo", "http://myns/echo")), "#ci04");
            Assert.IsTrue(xsdi.CanImport(xss, new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")), "#ci05");

            Assert.IsTrue(xsdi.CanImport(xss), "#ci06");

            Assert.IsTrue(xsdi.CanImport(xss,
                                         xss.GlobalElements [new XmlQualifiedName("dc", "http://schemas.datacontract.org/2004/07/")] as XmlSchemaElement),
                          "#ci07");

            Assert.IsTrue(xsdi.CanImport(xss,
                                         xss.GlobalElements [new XmlQualifiedName("Echo", "http://myns/echo")] as XmlSchemaElement),
                          "#ci08");
        }
        public void CanImportNullTest3()
        {
            XsdDataContractImporter xsdi = GetImporter();

            xsdi.CanImport(xss, (XmlSchemaElement)null);
        }
        public void CanImportNullTest2()
        {
            XsdDataContractImporter xsdi = GetImporter();

            xsdi.CanImport(xss, (XmlQualifiedName)null);
        }
        public void CanImportNullTest1()
        {
            XsdDataContractImporter xsdi = GetImporter();

            xsdi.CanImport(null);
        }