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

后端 未结 3 873
忘掉有多难
忘掉有多难 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:20

    I had no trouble producing the following:

    
    
        
            Hello
            5
        
    
    

    from

    [XmlInclude(typeof(FooClass))]
    //[XmlType(TypeName = "FooBase", Namespace = "urn:namespace", IncludeInSchema = true)]
    public class FooBaseClass
    {
        public string FooPropertyA { get; set; }
    }
    
    //[XmlType(TypeName = "Foo", Namespace = "urn:namespace", IncludeInSchema = true)]
    public class FooClass : FooBaseClass
    {
        public int FooPropertyB { get; set; } 
    }
    
    public class BazClass
    {
        public FooBaseClass Foo { get; set; }
    }
    

    (note the XmlType attributes are commented out. I wanted to see what happened if a namespace was specified)

    Please show the code you used, and the XML it produced.

提交回复
热议问题