简体   繁体   English

从生成xsd类更改ElementName

[英]Change ElementName from generate xsd class

Using the xsd.exe tool, class are generate from a xsd file. 使用xsd.exe工具,可以从xsd文件生成类。 I would like to change the name of a root element put I can not since the XmlRootElementAttribute can not be duplicate. 我想更改我不能更改的根元素的名称,因为XmlRootElementAttribute不能重复。 So idealy I would like that the xsd tool generate a partial classe like this: 所以理想情况下,我希望xsd工具生成这样的部分类:

...
[System.Xml.Serialization.XmlRootAttribute(ElementName="Request", IsNullable=false)]
public partial class SendMessage  { ... }

But I have no clue how to change the ElementName propertie. 但是我不知道如何更改ElementName属性。

Thanks 谢谢

I take your XSD defines an element SendMessage , and you want the annotation to be something else, Request in your case. 我用您的XSD定义了一个元素SendMessage ,并且您希望注释是其他内容,在您的情况下为Request

Short answer is no, it cannot be done with XSD.exe alone; 简短的答案是不,它不能仅通过XSD.exe来完成。 the tool doesn't allow a syntax where one can customize the name of the generated classes. 该工具不允许使用一种可以自定义所生成类名称的语法。

Which brings in the long version of the answer... The annotations always reflect what the XML is. 这带来了答案的冗长版本。注解始终反映XML是什么。 If you change the ElementName value, then your generated XML will be different. 如果更改ElementName值,则生成的XML将有所不同。 If you want the XML different, then you should change the schema file instead. 如果您希望XML不同,则应改为更改架构文件。

This is the kind of question that needs a lot of explaining as to why would one want this... in order to get a positive answer, as in "yes, this is how achieve the why ". 为了得到一个肯定的答案,这种问题需要大量解释, 为什么要这样……如“是的,这就是为什么要实现”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM