예제 #1
0
        // Note that this method doesn't use the LINQ To XML API since
        // no inference capabilities were added to it
        static void InferSchemaFromXml()
        {
            Print.Header();

            // Save the XML to disk
            xml.Save("Persons.xml");

            // Infer the scema set from the XML file
            XmlSchemaSet schemaSet = new XmlSchemaInference().InferSchema(new XmlTextReader("Persons.xml"));

            // Iterate through all schemas in the set (only one in this case)
            // and write the result to the XSD file
            using (XmlWriter writer = XmlWriter.Create("Persons.xsd"))
            {
                foreach (XmlSchema schema in schemaSet.Schemas())
                {
                    schema.Write(writer);
                }
            }

            // Load the schema from disk
            XDocument scemaDocument = XDocument.Load("Persons.xsd");

            Console.WriteLine(scemaDocument);
        }
예제 #2
0
        /// <summary>
        /// Pulls the XML out of a Configuration section, and then generates its XSD
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public XmlDetails GetXmlDetails(ConfigurationSection section)
        {
            try
            {
                var xml = section.SectionInformation.GetRawXml();
                if (string.IsNullOrWhiteSpace(xml))
                {
                    return(null);
                }
                //Get schema
                XmlReader     reader           = XmlReader.Create(new StringReader(xml));
                XmlSchemaSet  schemaSet        = new XmlSchemaInference().InferSchema(reader);
                var           schema           = schemaSet.Schemas().Cast <XmlSchema>().First();
                StringBuilder xmlSchemaBuilder = new StringBuilder();
                StringWriter  writer           = new StringWriter(xmlSchemaBuilder);
                schema.Write(writer);

                return(new XmlDetails()
                {
                    RawData = xml,
                    Schema = xmlSchemaBuilder.ToString()
                });
            }
            catch (Exception exc)
            {
                throw new MigrationException("There was an error while migrating the XML details", exc);
            }
        }
예제 #3
0
        public void GenerateSchemaFromXml(XmlReader inputXmlReader, XmlWriter outputXsdWriter)
        {
            var schemaSet = new XmlSchemaInference().InferSchema(inputXmlReader);

            if (schemaSet.Count > 1)
            {
                throw new Exception("Cannot write multiple schemas to single XmlWriter");
            }

            foreach (XmlSchema schema in schemaSet.Schemas())
            {
                schema.Write(outputXsdWriter);
            }
        }
예제 #4
0
        public void GenerateSchemaFromXml(string inputXmlFile, string outputXsdFile, bool overwriteIfExists = false)
        {
            XmlSchemaSet schemaSet;

            using (var reader = XmlReader.Create(inputXmlFile))
            {
                schemaSet = new XmlSchemaInference().InferSchema(reader);
            }

            var mode = overwriteIfExists ? FileMode.Create : FileMode.CreateNew;

            foreach (XmlSchema schema in schemaSet.Schemas())
            {
                using (var file = new FileStream(outputXsdFile, mode, FileAccess.Write))
                    using (var writer = XmlWriter.Create(file, new XmlWriterSettings {
                        Indent = true, Encoding = Encoding.UTF8
                    }))
                    {
                        schema.Write(writer);
                    }
            }
        }