使用 XSD.exe 自定义类名称生成的类

发布于 2024-07-15 06:14:20 字数 40 浏览 4 评论 0原文

是否可以对使用 .Net XSD.exe 工具生成的类名进行控制?

Is it possible to have any control over the class names that get generated with the .Net XSD.exe tool?

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

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

发布评论

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

评论(3

始终不够 2024-07-22 06:14:20

基本上,没有。 如果您手动编写类,则可以:

[XmlType("bar")]
class Foo {}

但是,您不能使用 xsd 生成的类来执行此操作。 不幸的是,您不能部分类做的事情之一就是重命名它。 当然,您可以使用xsd来生成它,更改.cs文件并且不再生成它,但这对于维护来说并不理想。

Basically, no. If you were writing the classes manually, you could have:

[XmlType("bar")]
class Foo {}

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 use xsd to generate it, change the .cs file and don't generate it again, but that is not ideal for maintenance.

北凤男飞 2024-07-22 06:14:20

据我所知,我认为这是不可能的,类名几乎与架构中的内容完全匹配。

就我个人而言,我会在 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?

眼趣 2024-07-22 06:14:20

任何具有一定深度嵌套的模式最终都会得到完全无用的名称。

我不知道有什么方法可以解决这个问题,但我至少减少负面影响的建议是:为名称糟糕的类型定义别名列表。 通过这种方式,您可以编写并非完全不可读的代码,而不会失去重新生成的能力。

using AgentAddress = Example.Namespace.DataContract.RootElementNestedElementAgentAddress;
...

遗憾的是这个列表本身必须复制粘贴到所有需要它的代码文件中,但我认为这至少构成了一种改进。

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.

using AgentAddress = Example.Namespace.DataContract.RootElementNestedElementAgentAddress;
...

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.

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