@@ -58,7 +58,8 @@ private static IEnumerable<string> ConvertXml(string name, IEnumerable<string> x
5858 CollectionSettersMode = generatorPrototype . CollectionSettersMode ,
5959 UseArrayItemAttribute = generatorPrototype . UseArrayItemAttribute ,
6060 EnumAsString = generatorPrototype . EnumAsString ,
61- AllowDtdParse = generatorPrototype . AllowDtdParse
61+ AllowDtdParse = generatorPrototype . AllowDtdParse ,
62+ OmitXmlIncludeAttribute = generatorPrototype . OmitXmlIncludeAttribute
6263 } ;
6364
6465 gen . CommentLanguages . Clear ( ) ;
@@ -3082,4 +3083,210 @@ List of Profiles
30823083 var exception = Assert . Throws < XmlException > ( ( ) => ConvertXml ( nameof ( TestNotAllowDtdParse ) , xsd , generator ) ) ;
30833084 Assert . Contains ( "Reference to undeclared entity 'lowalpha'" , exception . Message ) ;
30843085 }
3086+
3087+ [ Fact ]
3088+ public void TestOmitXmlIncludeAttribute ( )
3089+ {
3090+ const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
3091+ <xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:xlink=""http://www.w3.org/1999/xlink"" elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
3092+ <xs:complexType name=""BaseType"">
3093+ <xs:sequence>
3094+ <xs:element name=""BaseProperty"" type=""xs:string""/>
3095+ </xs:sequence>
3096+ </xs:complexType>
3097+ <xs:complexType name=""DerivedType1"">
3098+ <xs:complexContent>
3099+ <xs:extension base=""BaseType"">
3100+ <xs:sequence>
3101+ <xs:element name=""DerivedProperty1"" type=""xs:string""/>
3102+ </xs:sequence>
3103+ </xs:extension>
3104+ </xs:complexContent>
3105+ </xs:complexType>
3106+ <xs:complexType name=""DerivedType2"">
3107+ <xs:complexContent>
3108+ <xs:extension base=""BaseType"">
3109+ <xs:sequence>
3110+ <xs:element name=""DerivedProperty2"" type=""xs:string""/>
3111+ </xs:sequence>
3112+ </xs:extension>
3113+ </xs:complexContent>
3114+ </xs:complexType>
3115+ </xs:schema>" ;
3116+
3117+ // Test with OmitXmlIncludeAttribute = false (default behavior)
3118+ var generatorWithInclude = new Generator
3119+ {
3120+ NamespaceProvider = new NamespaceProvider
3121+ {
3122+ GenerateNamespace = key => "Test"
3123+ } ,
3124+ OmitXmlIncludeAttribute = false
3125+ } ;
3126+
3127+ var contentsWithInclude = ConvertXml ( nameof ( TestOmitXmlIncludeAttribute ) + "WithInclude" , xsd , generatorWithInclude ) ;
3128+ var contentWithInclude = Assert . Single ( contentsWithInclude ) ;
3129+
3130+ // Verify XmlIncludeAttribute is present for both derived types
3131+ Assert . Contains ( "[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedType1))]" , contentWithInclude ) ;
3132+ Assert . Contains ( "[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedType2))]" , contentWithInclude ) ;
3133+
3134+ // Test with OmitXmlIncludeAttribute = true
3135+ var generatorWithoutInclude = new Generator
3136+ {
3137+ NamespaceProvider = new NamespaceProvider
3138+ {
3139+ GenerateNamespace = key => "Test"
3140+ } ,
3141+ OmitXmlIncludeAttribute = true
3142+ } ;
3143+
3144+ var contentsWithoutInclude = ConvertXml ( nameof ( TestOmitXmlIncludeAttribute ) + "WithoutInclude" , xsd , generatorWithoutInclude ) ;
3145+ var contentWithoutInclude = Assert . Single ( contentsWithoutInclude ) ;
3146+
3147+ // Verify XmlIncludeAttribute is NOT present
3148+ Assert . DoesNotContain ( "XmlIncludeAttribute" , contentWithoutInclude ) ;
3149+
3150+ // Verify that the base and derived types are still generated correctly
3151+ Assert . Contains ( "public partial class BaseType" , contentWithoutInclude ) ;
3152+ Assert . Contains ( "public partial class DerivedType1 : BaseType" , contentWithoutInclude ) ;
3153+ Assert . Contains ( "public partial class DerivedType2 : BaseType" , contentWithoutInclude ) ;
3154+ }
3155+
3156+ [ Fact ]
3157+ public void TestOmitXmlIncludeAttributeSerializationWithExtraTypes ( )
3158+ {
3159+ const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
3160+ <xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://test.example/omit"" xmlns:t=""http://test.example/omit"" elementFormDefault=""qualified"">
3161+ <xs:element name=""Container"" type=""t:ContainerType""/>
3162+ <xs:complexType name=""ContainerType"">
3163+ <xs:sequence>
3164+ <xs:element name=""Item"" type=""t:BaseType""/>
3165+ </xs:sequence>
3166+ </xs:complexType>
3167+ <xs:complexType name=""BaseType"">
3168+ <xs:sequence>
3169+ <xs:element name=""BaseProperty"" type=""xs:string""/>
3170+ </xs:sequence>
3171+ </xs:complexType>
3172+ <xs:complexType name=""DerivedType1"">
3173+ <xs:complexContent>
3174+ <xs:extension base=""t:BaseType"">
3175+ <xs:sequence>
3176+ <xs:element name=""DerivedProperty1"" type=""xs:string""/>
3177+ </xs:sequence>
3178+ </xs:extension>
3179+ </xs:complexContent>
3180+ </xs:complexType>
3181+ <xs:complexType name=""DerivedType2"">
3182+ <xs:complexContent>
3183+ <xs:extension base=""t:BaseType"">
3184+ <xs:sequence>
3185+ <xs:element name=""DerivedProperty2"" type=""xs:int""/>
3186+ </xs:sequence>
3187+ </xs:extension>
3188+ </xs:complexContent>
3189+ </xs:complexType>
3190+ </xs:schema>" ;
3191+
3192+ var generator = new Generator
3193+ {
3194+ NamespaceProvider = new NamespaceProvider
3195+ {
3196+ GenerateNamespace = key => "Test.Omit"
3197+ } ,
3198+ OmitXmlIncludeAttribute = true
3199+ } ;
3200+
3201+ var contents = ConvertXml ( nameof ( TestOmitXmlIncludeAttributeSerializationWithExtraTypes ) , xsd , generator ) . ToArray ( ) ;
3202+ var assembly = Compiler . Compile ( nameof ( TestOmitXmlIncludeAttributeSerializationWithExtraTypes ) , contents ) ;
3203+
3204+ Assert . NotNull ( assembly ) ;
3205+
3206+ // Get the generated types
3207+ var containerType = assembly . GetType ( "Test.Omit.ContainerType" ) ;
3208+ var baseType = assembly . GetType ( "Test.Omit.BaseType" ) ;
3209+ var derivedType1 = assembly . GetType ( "Test.Omit.DerivedType1" ) ;
3210+ var derivedType2 = assembly . GetType ( "Test.Omit.DerivedType2" ) ;
3211+
3212+ Assert . NotNull ( containerType ) ;
3213+ Assert . NotNull ( baseType ) ;
3214+ Assert . NotNull ( derivedType1 ) ;
3215+ Assert . NotNull ( derivedType2 ) ;
3216+
3217+ // Verify that BaseType does NOT have XmlIncludeAttribute
3218+ var xmlIncludeAttributes = baseType . GetCustomAttributes ( typeof ( XmlIncludeAttribute ) , false ) ;
3219+ Assert . Empty ( xmlIncludeAttributes ) ;
3220+
3221+ // Test serialization with extraTypes parameter for DerivedType1
3222+ var serializer1 = new XmlSerializer ( containerType , new [ ] { derivedType1 , derivedType2 } ) ;
3223+
3224+ // Create an instance with DerivedType1
3225+ var container1 = Activator . CreateInstance ( containerType ) ;
3226+ var derived1 = Activator . CreateInstance ( derivedType1 ) ;
3227+
3228+ var basePropertyProp = baseType . GetProperty ( "BaseProperty" ) ;
3229+ var derivedProperty1Prop = derivedType1 . GetProperty ( "DerivedProperty1" ) ;
3230+
3231+ basePropertyProp . SetValue ( derived1 , "Base Value 1" ) ;
3232+ derivedProperty1Prop . SetValue ( derived1 , "Derived Value 1" ) ;
3233+
3234+ var itemProp = containerType . GetProperty ( "Item" ) ;
3235+ itemProp . SetValue ( container1 , derived1 ) ;
3236+
3237+ // Serialize
3238+ var sw1 = new StringWriter ( ) ;
3239+ serializer1 . Serialize ( sw1 , container1 ) ;
3240+ var xml1 = sw1 . ToString ( ) ;
3241+
3242+ // Verify the XML contains xsi:type information
3243+ Assert . Contains ( "DerivedType1" , xml1 ) ;
3244+ Assert . Contains ( "Base Value 1" , xml1 ) ;
3245+ Assert . Contains ( "Derived Value 1" , xml1 ) ;
3246+
3247+ // Deserialize back
3248+ var sr1 = new StringReader ( xml1 ) ;
3249+ var deserialized1 = serializer1 . Deserialize ( sr1 ) ;
3250+
3251+ Assert . NotNull ( deserialized1 ) ;
3252+ var deserializedItem1 = itemProp . GetValue ( deserialized1 ) ;
3253+ Assert . NotNull ( deserializedItem1 ) ;
3254+ Assert . Equal ( derivedType1 , deserializedItem1 . GetType ( ) ) ;
3255+ Assert . Equal ( "Base Value 1" , basePropertyProp . GetValue ( deserializedItem1 ) ) ;
3256+ Assert . Equal ( "Derived Value 1" , derivedProperty1Prop . GetValue ( deserializedItem1 ) ) ;
3257+
3258+ // Test serialization with DerivedType2
3259+ var serializer2 = new XmlSerializer ( containerType , new [ ] { derivedType1 , derivedType2 } ) ;
3260+
3261+ var container2 = Activator . CreateInstance ( containerType ) ;
3262+ var derived2 = Activator . CreateInstance ( derivedType2 ) ;
3263+
3264+ var derivedProperty2Prop = derivedType2 . GetProperty ( "DerivedProperty2" ) ;
3265+
3266+ basePropertyProp . SetValue ( derived2 , "Base Value 2" ) ;
3267+ derivedProperty2Prop . SetValue ( derived2 , 42 ) ;
3268+
3269+ itemProp . SetValue ( container2 , derived2 ) ;
3270+
3271+ // Serialize
3272+ var sw2 = new StringWriter ( ) ;
3273+ serializer2 . Serialize ( sw2 , container2 ) ;
3274+ var xml2 = sw2 . ToString ( ) ;
3275+
3276+ // Verify the XML contains xsi:type information
3277+ Assert . Contains ( "DerivedType2" , xml2 ) ;
3278+ Assert . Contains ( "Base Value 2" , xml2 ) ;
3279+ Assert . Contains ( "42" , xml2 ) ;
3280+
3281+ // Deserialize back
3282+ var sr2 = new StringReader ( xml2 ) ;
3283+ var deserialized2 = serializer2 . Deserialize ( sr2 ) ;
3284+
3285+ Assert . NotNull ( deserialized2 ) ;
3286+ var deserializedItem2 = itemProp . GetValue ( deserialized2 ) ;
3287+ Assert . NotNull ( deserializedItem2 ) ;
3288+ Assert . Equal ( derivedType2 , deserializedItem2 . GetType ( ) ) ;
3289+ Assert . Equal ( "Base Value 2" , basePropertyProp . GetValue ( deserializedItem2 ) ) ;
3290+ Assert . Equal ( 42 , derivedProperty2Prop . GetValue ( deserializedItem2 ) ) ;
3291+ }
30853292}
0 commit comments