問題描述
在 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 文檔的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!