将枚举序列化为 XML 属性

发布于 2024-12-08 16:23:01 字数 4187 浏览 0 评论 0原文

我从大量 XSD 中生成超过 100 个类,以构建我们的测试工具应用程序以进行内部测试。我使用 XSD.exe 来生成这些类,因为这样做会带来太大的风险,而且会花费太长时间。

我遇到了一个让我有点痛苦的问题。我正在尝试获取这个枚举(现在只是这个枚举,然后我将对其余的枚举执行相同的技术...)

我想要的是生成这样的东西(其中接收器元素有一个typeCode 属性):

<receiver typeCode="RCV">
    <device classCode="DEV" determinerCode="INSTANCE">
        <id root="..." extension="..." />
        <asAgent classCode="AGNT">
            <representedOrganization classCode="ORG" determinerCode="INSTANCE">
                <id root="..." extension="..." />
            </representedOrganization>
        </asAgent>
    </device>
</receiver>

但是,接收器永远不会获取属性,即使我已在测试工具启动对象中明确设置:

   this.receiver = new MCCI_MT000100BCReceiver();
   this.receiver.typeCode = CommunicationFunctionType.RSP;
   this.receiver.device = new MCCI_MT000100BCDevice();

我有一个枚举(我添加了 XMLEnums 和 [Flags] 属性,因为 XSD.exe 没有打扰)

[Flags]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:hl7-org:v3")]
public enum CommunicationFunctionType
{

    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("RCV")]
    RCV = 1,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("RSP")]
    RSP = 2,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("SND")]
    SND = 3,
}

这是一个使用该枚举的类:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(TypeName = "MCCI_MT000100BC.Receiver", Namespace = "urn:hl7-org:v3")]
    public partial class MCCI_MT000100BCReceiver
    {

        private CS[] realmCodeField;

        private II typeIdField;

        private II[] templateIdField;

        private MCCI_MT000100BCDevice deviceField;

        private CommunicationFunctionType typeCodeField;

        private bool typeCodeFieldSpecified;

        public MCCI_MT000100BCReceiver()
        {
            this.typeCodeField = CommunicationFunctionType.RCV;
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("realmCode")]
        public CS[] realmCode
        {
            get
            {
                return this.realmCodeField;
            }
            set
            {
                this.realmCodeField = value;
            }
        }

        /// <remarks/>
        public II typeId
        {
            get
            {
                return this.typeIdField;
            }
            set
            {
                this.typeIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("templateId")]
        public II[] templateId
        {
            get
            {
                return this.templateIdField;
            }
            set
            {
                this.templateIdField = value;
            }
        }

        /// <remarks/>
        public MCCI_MT000100BCDevice device
        {
            get
            {
                return this.deviceField;
            }
            set
            {
                this.deviceField = value;
            }
        }

        /// <remarks/>

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public CommunicationFunctionType typeCode
        {
            get
            {
                return this.typeCodeField;
            }
            set
            {
                this.typeCodeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool typeCodeSpecified
        {
            get
            {
                return this.typeCodeFieldSpecified;
            }
            set
            {
                this.typeCodeFieldSpecified = value;
            }
        }
    }

我知道 .NET 有时不喜欢序列化枚举,但任何帮助将不胜感激。

I'm generating >100 classes from a lot of XSD's to build our test harness application for internal testing. I'm using XSD.exe to generate these classes, as doing it by bring in too much risk and it would take way too long.

I'm having an issue that is causing me a bit of pain. I'm trying to get this enumeration (for now just this one, then I'll do the same technique on the remaining ones...)

What I would like is for this to generate something like this(where the receiver element has a typeCode attribute):

<receiver typeCode="RCV">
    <device classCode="DEV" determinerCode="INSTANCE">
        <id root="..." extension="..." />
        <asAgent classCode="AGNT">
            <representedOrganization classCode="ORG" determinerCode="INSTANCE">
                <id root="..." extension="..." />
            </representedOrganization>
        </asAgent>
    </device>
</receiver>

however, receiver never gets an attribute, even though I have explicetly set in the test harness startup object:

   this.receiver = new MCCI_MT000100BCReceiver();
   this.receiver.typeCode = CommunicationFunctionType.RSP;
   this.receiver.device = new MCCI_MT000100BCDevice();

I have an enumeration (I've added the XMLEnums and the [Flags] attribute as XSD.exe didn't bother)

[Flags]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:hl7-org:v3")]
public enum CommunicationFunctionType
{

    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("RCV")]
    RCV = 1,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("RSP")]
    RSP = 2,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("SND")]
    SND = 3,
}

And this is one of the classes that use that enum:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(TypeName = "MCCI_MT000100BC.Receiver", Namespace = "urn:hl7-org:v3")]
    public partial class MCCI_MT000100BCReceiver
    {

        private CS[] realmCodeField;

        private II typeIdField;

        private II[] templateIdField;

        private MCCI_MT000100BCDevice deviceField;

        private CommunicationFunctionType typeCodeField;

        private bool typeCodeFieldSpecified;

        public MCCI_MT000100BCReceiver()
        {
            this.typeCodeField = CommunicationFunctionType.RCV;
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("realmCode")]
        public CS[] realmCode
        {
            get
            {
                return this.realmCodeField;
            }
            set
            {
                this.realmCodeField = value;
            }
        }

        /// <remarks/>
        public II typeId
        {
            get
            {
                return this.typeIdField;
            }
            set
            {
                this.typeIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("templateId")]
        public II[] templateId
        {
            get
            {
                return this.templateIdField;
            }
            set
            {
                this.templateIdField = value;
            }
        }

        /// <remarks/>
        public MCCI_MT000100BCDevice device
        {
            get
            {
                return this.deviceField;
            }
            set
            {
                this.deviceField = value;
            }
        }

        /// <remarks/>

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public CommunicationFunctionType typeCode
        {
            get
            {
                return this.typeCodeField;
            }
            set
            {
                this.typeCodeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool typeCodeSpecified
        {
            get
            {
                return this.typeCodeFieldSpecified;
            }
            set
            {
                this.typeCodeFieldSpecified = value;
            }
        }
    }

I understand that serializing enumerations isn't something .NET likes to do at times, but any help would be appreciated.

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

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

发布评论

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

评论(1

漫雪独思 2024-12-15 16:23:01

.net/XML中的枚举没有问题;问题只是您(或 xsd)添加了“typeCodeSpecified”成员。这是一个用于有条件地包含值的模式 - 具体来说,对于名为“Foo”的成员,引擎会检查“FooSpecified”属性或“ShouldSerializeFoo()”方法。由于您从未将“typeCodeSpecified”设置为 true,因此它将返回 false,并且该成员将在序列化过程中被省略。

如果您不希望这样,请删除该成员。

There is no problem with enums in .net/XML; the problem is simply that you (or xsd) have added a "typeCodeSpecified" member. This is a pattern that is used to conditionally include values - specifically, for a member called "Foo", the engine checks for either a "FooSpecified" property, or a "ShouldSerializeFoo()" method. Since you never set "typeCodeSpecified" to true, it will return false, and the member will be omitted during serialization.

If you didn't want that, remove this member.

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