简体   繁体   English

是否有一个类从.NET中的XSD架构生成示例XML文档

[英]Is there a class to generate a sample XML document from XSD schema in .NET

In Visual Studio you can create a template XML document from an existing schema. 在Visual Studio中,您可以从现有模式创建模板XML文档。 The new XML Schema Explorer in VS2008 SP1 takes this a stage further and can create a sample XML document complete with data. VS2008 SP1中新的XML Schema Explorer进一步发挥了作用,可以创建一个包含数据的示例XML文档。 Is there a class library in .NET to do this automatically without having to use Visual Studio? .NET中是否有类库自动执行此操作而无需使用Visual Studio? I found the XmlSampleGenerator article on MSDN but it was written in 2004 so maybe there is something already included in .NET to do this now? 我在MSDN上找到了XmlSampleGenerator文章,但它是在2004年编写的,所以也许.NET中已经包含了一些东西来实现这一点?

some footwork is involved, but you could load the xsd into a DataSet object, iterate over the Tables and add a few rows in each by calling calling NewRow() on each and then adding those rows back into their respective tables.. then save the DataSet out to a file: 涉及到一些步法,但您可以将xsd加载到DataSet对象中,遍历Tables并在每个表中添加几行,方法是在每个上调用NewRow(),然后将这些行添加回各自的表中。然后保存DataSet输出到文件:

DataSet ds = new DataSet();
ds.ReadXmlSchema("c:/xsdfile.xsd");

foreach(DataTable t in ds.Tables)
{
var row = t.NewRow();
t.Rows.Add(row);
}

ds.WriteXml("c:/example.xml");

PS A little extra work, but instead of just iterating over each table type and adding empty rows, you could build a nice winform that would allow you to drop in some data for each of the rows. PS一些额外的工作,但不是只是迭代每个表类型并添加空行,你可以构建一个很好的winform,允许你为每一行输入一些数据。 I built something like this in about an hour a few weeks ago. 几个星期前我在大约一个小时内建造了这样的东西。

Have you tried http://xsd2code.codeplex.com/ ???? 你试过http://xsd2code.codeplex.com/ ????

It worked for me, it can work for you. 它对我有用,它可以为你工作。

Directly, none that I can think of, other than third party add-ons. 直接,没有我能想到的,除了第三方附加组件。 You could utilize the xsd schema definition tool to take your XSD and create a .NET object/class, once you have that, you could, to quote the linked page: 你可以使用xsd架构定义工具来获取你的XSD并创建一个.NET对象/类,一旦你有了它,你可以引用链接的页面:

XSD to Classes: Generates runtime classes from an XSD schema file. XSD到类:从XSD模式文件生成运行时类。 The generated classes can be used in conjunction with System.Xml.Serialization.XmlSerializer to read and write XML code that follows the schema. 生成的类可以与System.Xml.Serialization.XmlSerializer一起使用,以读取和写入遵循模式的XML代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM