Generate a test XML from XML Schema programmatically

后端 未结 2 796
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");
                }            
            }
    
    0 讨论(0)
  • 2020-12-10 00:33

    You can write simple function for put 1 row into your data table and after that execute DataTable.WriteXml(string filePath)

    Somethig like that:

    xmlschema1 schema=new xmlschema1();
    //put some test data in table
    schema.Persons.AddPersonsRow(...some params);
    //generate xml
    schema.Persons.WriteXml(filePath);
    
    0 讨论(0)
提交回复
热议问题