Add Xml Attribute to string property

前端 未结 2 888
春和景丽
春和景丽 2020-12-06 19:00

I have a custom object which has a string property called \'Name\' I\'d like to keep the XML generated by serialization the same but add an attribute to the element called \

2条回答
  •  生来不讨喜
    2020-12-06 19:16

    You can use a combination of XMLAttribute and XmlText()

    take below example of class declaration:

        public class Description {
        private int attribute_id;
        private string element_text;
    
        [XmlAttribute("id")]
        public int Id {
            get { return attribute_id; }
            set { attribute_id = value; }
        }
    
        [XmlText()]
        public string Text {
            get { return element_text; }
            set { element_text = value; }
        }
    }
    

    The output will be

    
    text
    

提交回复
热议问题