Generate a test XML from XML Schema programmatically

后端 未结 2 798
Happy的楠姐
Happy的楠姐 2020-12-09 23:47

I have searched for a bit now, but i\'m not able to find a way to autogenerate data from a XML Schema programmatically. Lets say I have this XML schema:

<         


        
2条回答
  •  温柔的废话
    2020-12-10 00:23

    after doing some searching. I have found a project that have implemented a xml sample generator. I created a test solution and imported the classes. Then i deleted the XmlGen.cs file and created my own main method. The output will be based on the root element.

    public static void Main(string[] args)
            {
                using (var stream = new MemoryStream(File.ReadAllBytes("schema.xsd")))
                {
                    var schema = XmlSchema.Read(XmlReader.Create(stream ), null);
                    var gen = new XmlSampleGenerator(schema, new XmlQualifiedName("rootElement"));
                    gen.WriteXml(XmlWriter.Create(@"c:\temp\autogen.xml"));
                    Console.WriteLine("Autogenerated file is here : c:\temp\autogen.xml");
                }            
            }
    

提交回复
热议问题