How can I force the use of an xsi:type attribute?

后端 未结 3 877
忘掉有多难
忘掉有多难 2020-12-20 07:44

How can I force .NET\'s XmlSerializer to add an xsi:type=\"FooClass\" to a member/node of type FooClass?

The scenario

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 08:29

    XmlSerializer can be pretty dumb and straightforward at times, which works to your advantage in this case. Just put it there manually:

    public class FooClass
    {
        public int FooPropertyA { get; set; }
        public string FooPropertyB { get; set; }
    
        [XmlAttribute("type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string XsiType
        {
            get { return "Foo"; }
            set { }
        }
    }
    

提交回复
热议问题