简体   繁体   中英

Deserialize an XML file with XmlSerializer but an attribute contains a colon in its name

I have this xml file that follows this dtd: http://www.edrdg.org/jmdict/jmdict_dtd_h.html

You can notice that 2 elements contains attributes with a colon ( : ) in their name:

lsource and gloss can contain an attribute named xml:lang , as seen in this example (for the lsource element):

<entry>
    <ent_seq>1002480</ent_seq>
    <k_ele>
        <keb>お転婆</keb>
    </k_ele>
    <k_ele>
        <keb>御転婆</keb>
    </k_ele>
    <r_ele>
        <reb>おてんば</reb>
    </r_ele>
    <sense>
        <pos>&adj-na;</pos>
        <pos>&n;</pos>
        <misc>&uk;</misc>
        <lsource xml:lang="dut">ontembaar</lsource>
        <gloss>tomboy</gloss>
    </sense>
</entry>

I am not how sure to define my class representing the lsource element, here it is for now, but it is missing this attribute:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.2046.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "JMdict_e.dtd")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "JMdict_e.dtd", IsNullable = false)]
    public partial class lsource
    {
        private string ls_typeField;

        private string ls_waseiField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string ls_type
        {
            get
            {
                return this.ls_typeField;
            }
            set
            {
                this.ls_typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string ls_wasei
        {
            get
            {
                return this.ls_waseiField;
            }
            set
            {
                this.ls_waseiField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

How should I name the property for the XmlSerializer to properly recognize and parse the attribute? I tried adding a property public string xml_lang { get; set; } public string xml_lang { get; set; } public string xml_lang { get; set; } or public string lang { get; set; } public string lang { get; set; } public string lang { get; set; } but both failed to parse the attribute from the xml file when XmlSerializer.Deserialize is called

That attribute is in a namespace that is not generated and therefor elements/attributes are happily ignored. Decorating the lang attribute with the namespace it is in will work:

[System.Xml.Serialization.XmlAttributeAttribute(Namespace = "http://www.w3.org/XML/1998/namespace")]
public string lang {
    get;set;
}

The xml namespace is a W3C defined standard namespace. Its value can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM