XSD generator from several XMLs

前端 未结 5 1031
南笙
南笙 2020-12-29 07:04

I know it is possible to generate skeleton XSD from XML. For example this post has good answers.

The question is how to generate XSD based on several

5条回答
  •  天命终不由人
    2020-12-29 07:59

    .Net 4.5 has schema inferencing...

    https://msdn.microsoft.com/en-us/library/xz2797k1(v=vs.110).aspx

    this can accept multiple sources!

    I needed this so I wrote the code, might as well share, pass in multiple file paths, first filepath is the xsd file to which you will write and the subsequent files are the input Xml files. This is a console application.

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace SchemaInferrer
    {
        class Program
        {
            static void Main(string[] args)
            {
                string xsdFile="";
                string[] xmlFiles=null;
                DivideArguments(args, ref xsdFile, ref xmlFiles);
    
                if (FilesExist(xmlFiles))
                {
                    Console.WriteLine("All files exist, good to infer...");
                    XmlSchemaSet schemaSet = new XmlSchemaSet();
                    XmlSchemaInference inference = new XmlSchemaInference();
    
    
                    bool bFirstTime = true;
                    foreach (string sFile in xmlFiles)
                    {
                        XmlReader reader = XmlReader.Create(sFile);
                        if (bFirstTime)
                        {
                            schemaSet = inference.InferSchema(reader);
                        } else
                        {
                            schemaSet = inference.InferSchema(reader, schemaSet );
                        }
                        bFirstTime = false;
                    }
    
    
                    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
                    {
                        Indent = true,
                        IndentChars = "\t"
                    };
    
                    XmlWriter writer = XmlWriter.Create(xsdFile, xmlWriterSettings);
    
                    foreach (XmlSchema schema in schemaSet.Schemas())
                    {
    
                        //schema.Write(Console.Out);
                        schema.Write(writer);
                    }
                    Console.WriteLine("Finished, wrote file to {0}...",xsdFile);
                    //Console.ReadLine();   
                }
    
            }
    
            static void DivideArguments(string [] args, ref string xsdFile, ref string[] xmlFiles)
            {
                xsdFile = args[0];
                xmlFiles=new string[args.Length-1];
    
                for (int i = 0; i < args.Length-1; i++)
                {
                    xmlFiles[i] = args[i + 1];
                }
            }
    
            static bool FilesExist(string[] args)
            {
                bool bFilesExist=true; //* until proven otherwise
    
                if (args.Length>0)
                {
                    foreach (string sFile in args )
                    {
                    if (!File.Exists(sFile) )
                        bFilesExist=false; 
                    }
                }
                return bFilesExist;
            }
        }
    }
    

提交回复
热议问题