从映射 C# .cs 文件或 NHibernate.hbm.xml 文件创建 DataSet.XSD 的工具或方法?

发布于 2024-12-27 18:05:48 字数 6748 浏览 0 评论 0原文

我需要根据 NHibernate.hbm.xml 文件或类文件创建 DataSet.xsd 文件。这些是我们用来处理数据库的映射文件和类文件。

您可能会问我,为什么我需要一个从 Nhibernate.hbm.xml 文件或 .cs 文件生成的 DataSet 文件?

这是因为我们正在使用 Crystal Reports,并且我们正在使用Frameworks、MVVM、INotifyPropertyChanged、NHibernate,并且经过大量研究,对于我们的情况,如果我们在程序执行时转换一个对象并使用它来生成报告。如果这个工具存在,它会变得更容易,因为我们的数据库中每个表有超过 60、70 列。由于我们已经将所有这些组件用于我们的项目,因此如果我们开始使用 sql 查询来生成报告就没有意义。

此工具或创建此数据集文件的“方式”需要读取以下格式的文件:

Nhibernate.hbm.xml 文件:

<?xml version="1.0" encoding="utf-8" ?>  
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="PCT.Domain" namespace="Gerdau.PCT.Kernel.Domain">
<class name="Furnace" table="Furnace" lazy="false">    
<id name="Id" column="Id_Furnace" type="Int64">
  <generator class="Geraes.GLib.GDomainBasis.CustomTableHiLoGenerator, GLib.GDomainBasis" />
</id>
<property name="Name" column="Name" type="String" length="50" not-null="true"/>
<property name="Code" column="Code" type="Char" not-null="true"/>
<property name="Mill" column="Mill" type="String" length="2" not-null="true"/>
<property name="DischEnabled" column="Disch_Enabled" type="Char" not-null="true"/>
<property name="DischEnabledTemp" column="Disch_Enabled_Temp" type="Char" not-null="true"/>
<property name="ChargeEnabled" column="Charge_Enabled" type="Char" not-null="true"/>
<property name="ChargeEnabledTemp" column="Charge_Enabled_Temp" type="Char" not-null="true"/>    
</class>  
</hibernate-mapping>

CSharp .cs 文件:

public class Furnace : BaseEntity
{
    public virtual String Name { get; set; }

    public virtual Char Code { get; set; }

    public virtual String Mill { get; set; }

    public virtual Char DischEnabled { get; set; }

    public virtual Boolean DischEnabledConv
    {
        get
        {
            return DischEnabled.ConvertYesNoToBoolean();
        }
        set
        {
            DischEnabled = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }

    public virtual Boolean DischEnabledTempConv
    {
        get
        {
            return DischEnabledTemp.ConvertYesNoToBoolean();
        }
        set
        {
            DischEnabledTemp = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }

    public virtual Char DischEnabledTemp { get; set; }

    public virtual Char ChargeEnabled { get; set; }

    public virtual Boolean ChargeEnabledConv
    {
        get
        {
            return ChargeEnabled.ConvertYesNoToBoolean();
        }
        set
        {
            ChargeEnabled = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }

    public virtual Boolean ChargeEnabledTempConv
    {
        get
        {
            return ChargeEnabledTemp.ConvertYesNoToBoolean();
        }
        set
        {
            ChargeEnabledTemp = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }
    public virtual Char ChargeEnabledTemp { get; set; }

    public virtual ConfiguracaoForno MillConv
    {
        get
        {
            if (Mill == ConfiguracaoForno.SM.ToString())
                return ConfiguracaoForno.SM;
            if (Mill == ConfiguracaoForno.PM.ToString())
                return ConfiguracaoForno.PM;
            else
                return ConfiguracaoForno.NN;
        }
    }

    public override String ToString()
    {
        return "Forno " + Code + " (" + Name + "): Laminador " + MillConv;
    }
}

I我知道这是一个非常具体的案例,但如果您至少能向我们展示一种方法,那将会有很大的帮助。

最好的问候,

Gustavo

编辑:

找到了一种方法:使用 XSD.EXE(Visual Studio 的内部工具),我从程序集中提取了映射:

C:\>xsd / c /l:CS -t:Furnace <MyAssembly>.dll -o:"D:\Temp"

但是现在,当我这样做时:

D:\>xsd /c schema0.xsd

导致这些错误:

D:\Temp>xsd /c schema0.xsd Microsoft (R) Xml 架构/数据类型支持实用程序 [Microsoft (R) .NET Framework,版本 4.0.30319.1] 版权所有 (C) 微软公司。保留所有权利。

<块引用>

架构验证警告:类型“char”未声明,或者不是简单类型。 21 号线,位置 5。 架构验证警告:未声明类型“http://microsoft.com/wsdl/types/:char”。 10 号线,位置 7。 架构验证警告:未声明类型“http://microsoft.com/wsdl/types/:char”。 12 号线,7 号位置。 架构验证警告:未声明类型“http://microsoft.com/wsdl/types/:char”。 15 号线,位置 7。 架构验证警告:未声明类型“http://microsoft.com/wsdl/types/:char”。 16 号线,位置 7。 架构验证警告:未声明类型“http://microsoft.com/wsdl/types/:char”。 19 号线,位置 7。 警告:无法验证架构。类生成可能会失败或可能产生不正确的结果。

错误:为架构“schema0”生成类时出错。 - 缺少数据类型“char”。

如果您需要更多帮助,请输入“xsd /?”。

这是我生成的 schema.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://microsoft.com/wsdl/types/" />
<xs:element name="Furnace" nillable="true" type="Furnace" />
<xs:complexType name="Furnace">
    <xs:complexContent mixed="false">
      <xs:extension base="BaseEntity">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
          <xs:element minOccurs="1" maxOccurs="1" name="Code" xmlns:q1="http://microsoft.com/wsdl/types/" type="q1:char" />
          <xs:element minOccurs="0" maxOccurs="1" name="Mill" type="xs:string" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabled" xmlns:q2="http://microsoft.com/wsdl/types/" type="q2:char" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabledConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabledTempConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabledTemp" xmlns:q3="http://microsoft.com/wsdl/types/" type="q3:char" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabled" xmlns:q4="http://microsoft.com/wsdl/types/" type="q4:char" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabledConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabledTempConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabledTemp" xmlns:q5="http://microsoft.com/wsdl/types/" type="q5:char" />
        </xs:sequence>
    </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:complexType name="BaseEntity">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="Id" type="xs:long" />
    </xs:sequence>
</xs:complexType>
</xs:schema>

非常感谢您的帮助。

古斯塔沃

I need to create a DataSet.xsd file according to a NHibernate.hbm.xml file or a Class File. These are Mapping files and class files than we use to work with our DB.

You may ask me, why I do need a DataSet file, generating from a Nhibernate.hbm.xml file or a .cs file?

It's because we're using Crystal Reports, and we're using Frameworks, MVVM, INotifyPropertyChanged, NHibernate and after a lot of studying, for our case, would be better if we Convert an object while in executing time of program and use it to generate a report. If this tool exists, it will make easier, 'cause we have over 60, 70 columns per table in our DB. Since we're already with all these components working into our Project, it wouldn't make sense if we start to use sql queries to generate reports.

This tool, or 'the way to' create this DataSet file, need to read those file in these formats:

Nhibernate.hbm.xml file:

<?xml version="1.0" encoding="utf-8" ?>  
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="PCT.Domain" namespace="Gerdau.PCT.Kernel.Domain">
<class name="Furnace" table="Furnace" lazy="false">    
<id name="Id" column="Id_Furnace" type="Int64">
  <generator class="Geraes.GLib.GDomainBasis.CustomTableHiLoGenerator, GLib.GDomainBasis" />
</id>
<property name="Name" column="Name" type="String" length="50" not-null="true"/>
<property name="Code" column="Code" type="Char" not-null="true"/>
<property name="Mill" column="Mill" type="String" length="2" not-null="true"/>
<property name="DischEnabled" column="Disch_Enabled" type="Char" not-null="true"/>
<property name="DischEnabledTemp" column="Disch_Enabled_Temp" type="Char" not-null="true"/>
<property name="ChargeEnabled" column="Charge_Enabled" type="Char" not-null="true"/>
<property name="ChargeEnabledTemp" column="Charge_Enabled_Temp" type="Char" not-null="true"/>    
</class>  
</hibernate-mapping>

CSharp .cs File:

public class Furnace : BaseEntity
{
    public virtual String Name { get; set; }

    public virtual Char Code { get; set; }

    public virtual String Mill { get; set; }

    public virtual Char DischEnabled { get; set; }

    public virtual Boolean DischEnabledConv
    {
        get
        {
            return DischEnabled.ConvertYesNoToBoolean();
        }
        set
        {
            DischEnabled = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }

    public virtual Boolean DischEnabledTempConv
    {
        get
        {
            return DischEnabledTemp.ConvertYesNoToBoolean();
        }
        set
        {
            DischEnabledTemp = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }

    public virtual Char DischEnabledTemp { get; set; }

    public virtual Char ChargeEnabled { get; set; }

    public virtual Boolean ChargeEnabledConv
    {
        get
        {
            return ChargeEnabled.ConvertYesNoToBoolean();
        }
        set
        {
            ChargeEnabled = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }

    public virtual Boolean ChargeEnabledTempConv
    {
        get
        {
            return ChargeEnabledTemp.ConvertYesNoToBoolean();
        }
        set
        {
            ChargeEnabledTemp = ((Boolean)value).ConvertYesNoToBoolean();
        }
    }
    public virtual Char ChargeEnabledTemp { get; set; }

    public virtual ConfiguracaoForno MillConv
    {
        get
        {
            if (Mill == ConfiguracaoForno.SM.ToString())
                return ConfiguracaoForno.SM;
            if (Mill == ConfiguracaoForno.PM.ToString())
                return ConfiguracaoForno.PM;
            else
                return ConfiguracaoForno.NN;
        }
    }

    public override String ToString()
    {
        return "Forno " + Code + " (" + Name + "): Laminador " + MillConv;
    }
}

I know it's an very specific case, but if you can show to us at least a way, it'll be of great help.

Best regards,

Gustavo

Edit:

Found a way to do it: using XSD.EXE, a internal Tool from Visual Studio, i've extracted the mapping from my assembly:

C:\>xsd / c /l:CS -t:Furnace <MyAssembly>.dll -o:"D:\Temp"

But now, when I do this:

D:\>xsd /c schema0.xsd

Results in these errors:

D:\Temp>xsd /c schema0.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.

Schema validation warning: Type 'char' is not declared, or is not a simple type.
Line 21, position 5.
Schema validation warning: Type 'http://microsoft.com/wsdl/types/:char' is not declared. Line 10, position 7.
Schema validation warning: Type 'http://microsoft.com/wsdl/types/:char' is not declared. Line 12, position 7.
Schema validation warning: Type 'http://microsoft.com/wsdl/types/:char' is not declared. Line 15, position 7.
Schema validation warning: Type 'http://microsoft.com/wsdl/types/:char' is not declared. Line 16, position 7.
Schema validation warning: Type 'http://microsoft.com/wsdl/types/:char' is not declared. Line 19, position 7.
Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

Error: Error generating classes for schema 'schema0'.
- The datatype 'char' is missing.

If you would like more help, please type "xsd /?".

Here's my generated schema.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://microsoft.com/wsdl/types/" />
<xs:element name="Furnace" nillable="true" type="Furnace" />
<xs:complexType name="Furnace">
    <xs:complexContent mixed="false">
      <xs:extension base="BaseEntity">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
          <xs:element minOccurs="1" maxOccurs="1" name="Code" xmlns:q1="http://microsoft.com/wsdl/types/" type="q1:char" />
          <xs:element minOccurs="0" maxOccurs="1" name="Mill" type="xs:string" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabled" xmlns:q2="http://microsoft.com/wsdl/types/" type="q2:char" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabledConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabledTempConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="DischEnabledTemp" xmlns:q3="http://microsoft.com/wsdl/types/" type="q3:char" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabled" xmlns:q4="http://microsoft.com/wsdl/types/" type="q4:char" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabledConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabledTempConv" type="xs:boolean" />
          <xs:element minOccurs="1" maxOccurs="1" name="ChargeEnabledTemp" xmlns:q5="http://microsoft.com/wsdl/types/" type="q5:char" />
        </xs:sequence>
    </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:complexType name="BaseEntity">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="Id" type="xs:long" />
    </xs:sequence>
</xs:complexType>
</xs:schema>

I appreciate for any help.

Gustavo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2025-01-03 18:05:48

您缺少 XSD 文件。

更新:假设您继续使用 xsd.exe。在这种情况下,修改导入语句如下(我将此文件称为 XSD-1.xsd):

<xs:import namespace="http://microsoft.com/wsdl/types/" schemaLocation="XSD-2.xsd" />

将以下内容复制到同一文件夹中的 XSD-2.xsd 文件中:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://microsoft.com/wsdl/types/" elementFormDefault="qualified" targetNamespace="http://microsoft.com/wsdl/types/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="guid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="char">
    <xs:restriction base="xs:unsignedShort" />
  </xs:simpleType>
</xs:schema>

以下行运行 xsd.exe:

xsd XSD-1.xsd /c

使用 应该得到一个 XSD-1.cs 文件和一些警告

如果你想使用 svcutil,你必须有 XSD-2.xsd,否则它将无法工作;另外,从好的方面来说,您不必修改 XSD-1.xsd:

svcutil XSD-1.xsd XSD-2.xsd /dconly

它工作正常,没有任何错误。我正在运行 v4.

You're missing an XSD file.

UPDATE: Let's assume you're staying with xsd.exe. In this case, modify the import statement as following (I call this file XSD-1.xsd):

<xs:import namespace="http://microsoft.com/wsdl/types/" schemaLocation="XSD-2.xsd" />

Copy the content below in XSD-2.xsd file, in the same folder:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://microsoft.com/wsdl/types/" elementFormDefault="qualified" targetNamespace="http://microsoft.com/wsdl/types/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="guid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="char">
    <xs:restriction base="xs:unsignedShort" />
  </xs:simpleType>
</xs:schema>

Run the xsd.exe with the following line:

xsd XSD-1.xsd /c

You should get an XSD-1.cs file and some warnings

If you want to use svcutil, you must have the XSD-2.xsd, otherwise it won't work; also, on the plus side you don't have to modify the XSD-1.xsd:

svcutil XSD-1.xsd XSD-2.xsd /dconly

It works fine, without any errors. I am running v4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文