通过代码将XDocument转换为xsd文件



可能的重复:
如何在C#中以编程方式创建XSD文件?

我有一个 XDocument 对象,我想通过代码将其转换为 xsd 文件

我应该怎么做?

我的项目如下所示:

WebClient client = new WebClient(); 
Stream stream = client.OpenRead("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Vancouver+BC&mode=bicycling&language=fr-FR&sensor=false"); 
XDocument doc = XDocument.Load(stream); 

如果对象中加载XML符合XSD格式,只需将其保存在XSD扩展名中XDocument即可。

WebClient client = new WebClient(); 
Stream stream = client.OpenRead("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Vancouver+BC&mode=bicycling&language=fr-FR&sensor=false"); 
XDocument doc = XDocument.Load(stream);
// ...
doc.Save(@"C:AnyFileName.xsd");

添加:或者,如果XML不是XSD格式,则可以使用以下代码段根据输入XML生成XSD

doc.Save(@"C:xmlFile.xml");
string parms = @"C:File.xml /outputdir:C:\";
string xsdExePath = @"C:Program Files...xsd.exe";
ProcessStartInfo psi = new ProcessStartInfo(xsdExePath, parms);
var process = System.Diagnostics.Process.Start(psi);

现在,您可以在 C:\ 驱动器根目录下获得XSD

最新更新