問(wèn)題描述
在 Visual Studio 中,您可以從現(xiàn)有架構(gòu)創(chuàng)建模板 XML 文檔.VS2008 SP1 中新的 XML Schema Explorer 更進(jìn)一步,可以創(chuàng)建一個(gè)包含數(shù)據(jù)的示例 XML 文檔..NET 中是否有一個(gè)類(lèi)庫(kù)可以自動(dòng)執(zhí)行此操作而無(wú)需使用 Visual Studio?我在 MSDN 上找到了 XmlSampleGenerator 文章,但它寫(xiě)于 2004 年,所以可能現(xiàn)在 .NET 中已經(jīng)包含了一些東西可以做到這一點(diǎn)?
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 對(duì)象中,遍歷表并通過(guò)調(diào)用 NewRow() 在每個(gè)表中添加幾行在每個(gè)上,然后將這些行添加回各自的表中.然后將 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");
附:一些額外的工作,但不是只迭代每個(gè)表類(lèi)型并添加空行,您可以構(gòu)建一個(gè)不錯(cuò)的 winform,允許您為每行刪除一些數(shù)據(jù).幾周前,我在大約一個(gè)小時(shí)內(nèi)構(gòu)建了這樣的東西.
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.
這篇關(guān)于是否有一個(gè)類(lèi)可以從 .NET 中的 XSD 模式生成示例 XML 文檔的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!