从 xsd 生成可序列化的 cs 类:使用哪个工具?

发布于 2024-10-10 14:16:17 字数 1273 浏览 0 评论 0原文

我需要定义一些实现第三方 xsd 定义的类。 xsd 相当复杂,有很多限制结束枚举,因此我尝试使用 xsd.exe 和 xsd2code 来节省一些时间。然而,生成的类不考虑 minoccurrs=0 属性,最糟糕的是,映射到枚举(int),某些类型定义为字符串。例如,下面的类型被映射为具有相同名称的枚举:

<xsd:element name="CodiceFornitura">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="IMP00"/>
                </xsd:restriction>
            </xsd:simpleType>
    </xsd:element>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public enum CodiceFornitura
    {
        /// <remarks/>
        IMP00,
    }

另一个问题是某些元素可能不会被序列化:例如,如果我要发送一种类型的文档(例如 OriginalInvoiceUpdate),我必须添加一个片段的 xml 声明必须替换哪个发票,否则必须省略该 xml 片段。

xsd2code 有两个属性来生成代码:GenerateShouldSerialize 和GeneratePropertyNameSpecified。将第一个设置为 true 没有任何效果,将第二个设置为 All 会生成所有类成员的属性,而不仅仅是 minoccurrs=0 的类成员。

所以问题是: 1.我是否应该更改生成的源以反映我的需求,xsd.exe 或 xsd2code 缺少一些配置? 2. XmlSerializer 将在枚举的 xml 中写入什么:枚举值(“1”)或值的字符串表示形式(IMP00)? 3. 还有其他工具可以从 xsd 构建源代码吗?

预先感谢您的帮助

i need to define some classes that implement a third party xsd definitions . The xsd is quite complex , with a lot of restrictions end enumerations, Therefore i've tried to save a bit of time using xsd.exe and xsd2code. However generated classes don't take into account the minoccurs=0 attribute and , worst, maps to an enum (int) some types defined as string. For example the type below is mapped as an enumeration with the same name :

<xsd:element name="CodiceFornitura">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="IMP00"/>
                </xsd:restriction>
            </xsd:simpleType>
    </xsd:element>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public enum CodiceFornitura
    {
        /// <remarks/>
        IMP00,
    }

The other problem is that some element migth or might not be serialized :for example if i'm sending a type of document (say OriginalInvoiceUpdate), i must add a piece of xml that states which invoce has to be replaced otherwise that piece of xml must be omitted.

xsd2code has two properties to generate code : GenerateShouldSerialize and GeneratePropertyNameSpecified. Setting the first to true don't have any effect, setting the second to All generates the properties for all class memmbers , not only for thats with minoccurs=0.

So the questions are :
1. shall i change the generated sources to reflect my needs, there are some configurations i'm missing with xsd.exe or xsd2code ?
2. what XmlSerializer will write into the xml for enums : the enumeration value ("1") or the string representation of the value (IMP00) ?
3. there are other tools to build sources from xsd ?

thank you in advance for any help

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

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

发布评论

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

评论(2

一笔一画续写前缘 2024-10-17 14:16:17

查看 svcutil。它具有从 xsd 生成类的选项,并且比 xsd.exe 更易于配置。

Have a look at svcutil. It has options to generate classes from xsd and is a lot more configurable than xsd.exe.

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