【问题标题】:How to Save a Generated XSD File如何保存生成的 XSD 文件
【发布时间】:2014-06-25 12:29:59
【问题描述】:

我有一些代码(在 c# 中)可以即时创建一堆 XML 表。在我的代码末尾,我将基于这些 XML 表生成 XSD。我正在成功制作 XSD,但将它们保存为文件是我无法弄清楚的。到目前为止,我的代码基本上取自 MSDN page on generate XSDs from XML sheet:

        Directory.CreateDirectory(directoryName);
        string[] directoryFiles = Directory.GetFiles(xmlFilePath);

        foreach (string xFile in directoryFiles)
        {
            XmlReader reader = XmlReader.Create(xFile);
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            XmlSchemaInference schema = new XmlSchemaInference();

            schema.TypeInference = XmlSchemaInference.InferenceOption.Relaxed;

            schemaSet = schema.InferSchema(reader);

            //insert code here to save the file
            //stored in schemaSet.Schemas()
        }

感谢任何帮助。谢谢。

【问题讨论】:

  • 如果您只想从 XML 文件生成 XSD 文件,您可以使用 Microsoft 的 XSD.EXE,它包含在 Visual Studio 中。
  • 您不必从命令行手动运行它吗?此代码将按计划运行,我需要每次动态生成这些 XSD。
  • 你是对的,这是从 Visual Studio 命令行运行的。但是,如果您想动态生成它们,this 可能会有所帮助。

标签: c# xml linq xsd


【解决方案1】:

XMLSchemaSet 有一个名为 Schemas() 的方法,它返回集合中所有模式的集合。 MSDN 有一个页面描述了如何访问这些。

只需使用上面链接中的代码访问集合中的每个模式,并使用示例here 编写它。

【讨论】:

    猜你喜欢
    • 2012-05-23
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2010-10-15
    • 2014-02-16
    相关资源
    最近更新 更多