是否有一个类可以从 .NET 中的 XSD 模式生成示例 XML 文档 [英] Is there a class to generate a sample XML document from XSD schema in .NET

查看:24
本文介绍了是否有一个类可以从 .NET 中的 XSD 模式生成示例 XML 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 中,您可以从现有架构创建模板 XML 文档.VS2008 SP1 中新的 XML Schema Explorer 更进一步,可以创建一个包含数据的示例 XML 文档..NET 中是否有一个类库可以自动执行此操作而无需使用 Visual Studio?我在 MSDN 上找到了 XmlSampleGenerator 文章,但它写于 2004 年,所以可能现在 .NET 中已经包含了一些东西可以做到这一点?

In Visual Studio you can create a template XML document from an existing schema. The new XML Schema Explorer in VS2008 SP1 takes this a stage further and can create a sample XML document complete with data. Is there a class library in .NET to do this automatically without having to use 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?

推荐答案

涉及一些步骤,但是您可以将 xsd 加载到 DataSet 对象中,遍历表并通过调用 NewRow() 在每个表中添加几行在每个上,然后将这些行添加回各自的表中.然后将 DataSet 保存到文件中:

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:

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");

附:一些额外的工作,但不是只迭代每个表类型并添加空行,您可以构建一个不错的 winform,允许您为每行删除一些数据.几周前,我在大约一个小时内构建了这样的东西.

P.S. 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. I built something like this in about an hour a few weeks ago.

这篇关于是否有一个类可以从 .NET 中的 XSD 模式生成示例 XML 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆