【问题标题】:How to programmatically convert schema to C# class in .NET core如何以编程方式将模式转换为 .NET Core 中的 C# 类
【发布时间】:2022-11-26 06:46:45
【问题描述】:

我正在寻找一种将模式转换为 .NET 核心中的 C# 类的方法。 在 .NET 框架中,我使用 XmlCodeExporter 类来实现此目的,但似乎尚未移植到 .NET 例如,这是一个简单的 shema:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="sometype">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="somestring" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="sometype" nillable="true" type="sometype" />
</xs:schema>

我可以运行 xsd 工具来生成以下类:xsd.exe Schema.xsd /c

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
public partial class sometype {
    
    private string somestringField;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string somestring {
        get {
            return this.somestringField;
        }
        set {
            this.somestringField = value;
        }
    }
}

我怎样才能以编程方式实现这一目标?

在此先感谢您提供的任何帮助

【问题讨论】:

    标签: .net xsd schema


    【解决方案1】:

    微软过去有一个名为 LinqToXsd 的工具,它本质上是 XSD.exe 的一个更好版本。

    虽然他们自己不再支持它,但它已被社区作为 LinqToXsdCore 移植到 .NET 核心。它在 GitHub 上:https://github.com/mamift/LinqToXsdCore

    它在 .NET Core 上运行,但它生成的代码以 .NET Standard 2.0 为目标,因此您可以使用其代码以 .NET Framework/.NET Core 为目标。

    编辑:如果您检查https://github.com/mamift/LinqToXsdCore/blob/master/XObjectsCode/Src/XObjectsCoreGenerator.cs,它具有可以编程方式访问的代码生成工具

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2013-02-05
      • 1970-01-01
      相关资源
      最近更新 更多