开发者

Serialize an Enumeration to an XML Attribute

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 class开发者_Go百科Code="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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜