是否有一个类可以从 .NET 中的 XSD 模式生成示例 XML 文档

4

本文介绍了是否有一个类可以从 .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 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14

函数委托与函数
Func Delegate vs Function(函数委托与函数)...
2023-11-11 C#/.NET开发问题
6

具有未知类型的 CreateDelegate
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)...
2023-11-11 C#/.NET开发问题
5