从C#类文件生成.xsd,然后从它生成的.xsd文件生成类 [英] Generating .xsd from C# class file, then generate class from the .xsd file it generated
问题描述
我们丢失了所有.xsd文件,只留下了C#类文件。我正在使用xsd.exe从C#类文件生成.xsd文件,但是当我使用xsd.exe从这些生成的.xsd文件生成回C#类时,新的C#类文件与原来的
文件不同。
We lost all .xsd files, only have C# classes files left. I'm using xsd.exe to generate .xsd files from C# classes files, but when I use xsd.exe to generate back C# classes from these generated .xsd files, new C# class files are different from the original ones.
为了说明我的问题,我简化了以下代码:
In order to illustrate my problem, I simplified code as below:
VisualStuio 2008
VisualStuio 2008
项目name:web,项目类型:C#类库
Project name: web, project type: C# Class Library
构建输出:web.dll
Build output: web.dll
2个源文件:
文件#1:Response.cs (包含基类)
File #1: Response.cs (contains base class)
namespace VWS.Types
{
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
//[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Response
{
}
}
文件#2:ResponseTSIRetrieveList.cs
File #2: ResponseTSIRetrieveList.cs
namespace VWS.Types
{
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
//[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ResponseTSIRetrieveList : Response
{
}
}
第1步:生成来自ResponseTSIRetrieveList类的.xsd文件:
Step 1: Generate .xsd file from ResponseTSIRetrieveList class:
&NBSP; &NBSP; &NBSP; &NBSP; xsd.exe -t:ResponseTSIRetrieveList web.dll
xsd.exe -t:ResponseTSIRetrieveList web.dll
&NBSP; &NBSP; &NBSP; &NBSP;生成了schema0.xsd文件。
schema0.xsd file is generated.
步骤2:从.xsd文件生成C#类:
Step2: Generate C# class from the .xsd file:
&NBSP; &NBSP; &NBSP; xsd.exe -c schema0.xsd &NBSP; &NBSP;
xsd.exe -c schema0.xsd
这是生成的C#文件:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.1432.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
public partial class ResponseTSIRetrieveList : Response {
}
/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(ResponseTSIRetrieveList))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class Response {
}
如您所见,基类和派生类都在同一个文件中生成。我该如何避免这种情况?
As you can see, both base class and derived class are generated in same file. How can I avoid this?
非常感谢提前
推荐答案
为什么要将它们放在不同的文件中?输出对编码没有任何影响。据我所知,.xsd文件没有任何属性来指定哪个文件包含某些类。您需要查找有关xsd.exe的文档。
Why do you want them in different files? The output doesn't have any influence with coding. As far as I know, .xsd files doesn't have any properties to specify which file contains certain classes. you need to look up the documentation about xsd.exe.
这篇关于从C#类文件生成.xsd,然后从它生成的.xsd文件生成类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!