使用 XSD.exe 自定义类名称生成的类
是否可以对使用 .Net XSD.exe 工具生成的类名进行控制?
Is it possible to have any control over the class names that get generated with the .Net XSD.exe tool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,没有。 如果您手动编写类,则可以:
但是,您不能使用 xsd 生成的类来执行此操作。 不幸的是,您不能对
部分类
做的事情之一就是重命名它。 当然,您可以使用xsd
来生成它,更改.cs文件并且不再生成它,但这对于维护来说并不理想。Basically, no. If you were writing the classes manually, you could have:
however, you can't do this with the xsd-generated classes. Unfortunately, one of the things you can't do with a
partial class
is rename it. Of course, you could usexsd
to generate it, change the .cs file and don't generate it again, but that is not ideal for maintenance.据我所知,我认为这是不可能的,类名几乎与架构中的内容完全匹配。
就我个人而言,我会在 XSD 生成代码后更改类名,但说实话,我通常只坚持使用 XSD 生成的内容。 这样,其他阅读代码的人就可以更轻松地理解哪些类映射到 XML 的哪些部分。
或者,如果您可以控制架构,您可以更新它吗?
As far as I'm aware I don't think this is possible, the class names match almost exactly to whats in the schema.
Personally I would change the class names after XSD has generated the code, but to be honest I usually just stick with what XSD generates. Its then easier for someone else reading the code to understand what classes map to what parts of the XML.
Alternatively, if you have control over the schema you could update that?
任何具有一定深度嵌套的模式最终都会得到完全无用的名称。
我不知道有什么方法可以解决这个问题,但我至少减少负面影响的建议是:为名称糟糕的类型定义别名列表。 通过这种方式,您可以编写并非完全不可读的代码,而不会失去重新生成的能力。
遗憾的是这个列表本身必须复制粘贴到所有需要它的代码文件中,但我认为这至少构成了一种改进。
Any schema with somewhat deep nesting then ends up with utterly useless names.
I don't know of a way to work around the problem, but my tip to at least reduce the negative impact is this: Define a list of aliases for the awfully-named types. This way you can write code that isn't completely unreadable without losing the ability to regenerate.
It's a pity this list itself has to be copy-pasted to all code files needing it, but I think this at least constitutes an improvement.