我可以在 XSD 生成的类中对整数进行零填充吗? [英] Can I zero-pad an integer in an XSD-generated class?
问题描述
情况是这样的:我有一个客户发给我一个 XML 模式,我的软件将他们的记录从制表符分隔转换为 XML.
Here's the situation: I have a client who's issued me an XML schema, and my software converts their records from tab-delimited to XML.
其中一个字段文件序列"在模式中被输入为整数.但是,客户端的客户端(集成目标)希望在实际输出 XML 中使用零填充和 4 位长度的整数(EG,
).
One of those fields, "file-sequence," is typed as an integer in the schema. However, the client's client (the integration target) wants that integer zero-padded and 4 digits long (EG, <file-sequence>0001</file-sequence>
) in the actual output XML.
我正在使用序列化输出,这很好、自动且无痛.但是,因为该字段在 XSD 中被输入为整数,所以文件序列字段显示为
.这是有道理的.:)
I'm outputting using serialization, which is nice and automatic and painless. However, because the field is typed as an integer in the XSD, the file-sequence field comes out as <file-sequence>1</file-sequence>
. Which makes sense. :)
到目前为止,我已将可能的答案缩小到三个选项:
So far I've narrowed potential answers down to three options:
编辑架构并将类型更改为字符串.更新字段时用零填充序列号.缺点:我必须记住在集成目标更改架构时执行此操作(因为它们已经更改了大约六次).
Edit the schema and change the type to string. Zero-pad the sequence number upon updating the field. Downside: I have to remember to do this anytime the integration target changes the schema (as they have about a half-dozen times already).
放弃使用 XML 序列化,在代码中手动生成 XML.缺点:大量工作,可能容易出错,严重的代码味道(对我来说).
Forego using XML serialization, manually generating the XML in code. Downside: A lot of work, possibly error-prone, a serious code-smell (to me).
序列化为内存流,从中取出原始 XML,填充整数.缺点:感觉很脏,还有很多工作要做.
Serialize to an in-memory stream, get the raw XML out of that, pad the integer there. Downsides: Feels really dirty, still a lot of work.
还有其他选择吗?如果没有,这里的哪个选项是正确的方法?(我认为选项 1 可能是最干净的.)
Are there any other options? If not, which option here is the right way to do this? (I'm thinking option 1 is probably cleanest.)
推荐答案
老实说,选项 1 是唯一真正的选项.如果目标关心字符串中的字符数,则该字段不是整数;这是一个仅限于数字的字符串.
Honestly, option 1 is the only real option. If the target cares about the number of characters in the string, then the field is not an integer; it's a string that's restricted only to numeric digits.
这篇关于我可以在 XSD 生成的类中对整数进行零填充吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!