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:
我现在搜索了一下,但是我无法找到一种以编程方式从XML Schema自动生成数据的方法。假设我有这个XML架构:
<xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" name ="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
I am able to create a XML from this using the VS function "Generate Sample XML"
Is there a way to do this programmatically?
我可以使用VS函数“生成示例XML”从此创建XML有没有办法以编程方式执行此操作?
Edit: To specify. I do not want to create all the objects and insert data programmatically myself. I would like for it to create the objects and attributes automatically just like the "Generate Sample XML" in VS. The reason for this is that i would like to change the XSD without having to do anything about xml sample generation.
编辑:指定。我不想自己创建所有对象并以编程方式插入数据。我希望它能像VS中的“生成示例XML”一样自动创建对象和属性。这样做的原因是我想更改XSD而不必对xml样本生成做任何事情。
2 个解决方案
#1
9
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.
经过一番搜索。我找到了一个实现了xml样本生成器的项目。我创建了一个测试解决方案并导入了类。然后我删除了XmlGen.cs文件并创建了我自己的main方法。输出将基于根元素。
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");
}
}
#2
0
You can write simple function for put 1 row into your data table and after that execute DataTable.WriteXml(string filePath)
您可以编写简单的函数,将1行放入数据表,然后执行DataTable.WriteXml(string filePath)
Somethig like that:
Somethig喜欢这样:
xmlschema1 schema=new xmlschema1();
//put some test data in table
schema.Persons.AddPersonsRow(...some params);
//generate xml
schema.Persons.WriteXml(filePath);
#1
9
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.
经过一番搜索。我找到了一个实现了xml样本生成器的项目。我创建了一个测试解决方案并导入了类。然后我删除了XmlGen.cs文件并创建了我自己的main方法。输出将基于根元素。
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");
}
}
#2
0
You can write simple function for put 1 row into your data table and after that execute DataTable.WriteXml(string filePath)
您可以编写简单的函数,将1行放入数据表,然后执行DataTable.WriteXml(string filePath)
Somethig like that:
Somethig喜欢这样:
xmlschema1 schema=new xmlschema1();
//put some test data in table
schema.Persons.AddPersonsRow(...some params);
//generate xml
schema.Persons.WriteXml(filePath);