How to add attributes for C# XML Serialization

前端 未结 4 1928
心在旅途
心在旅途 2020-11-30 03:42

I am having an issue with serializing and object, I can get it to create all the correct outputs except for where i have an Element that needs a value and an attribute. Here

4条回答
  •  萌比男神i
    2020-11-30 04:10

    It sounds like you need an extra class:

    public class Document
    {
        [XmlAttribute("type")]
        public string Type { get; set; }
        [XmlText]
        public string Name { get; set; }
    }
    

    Where an instance (in the example) would have Type = "word" and Name = "document name"; documents would be a List.

    By the way - public fields are rarely a good idea...

提交回复
热议问题