Example #1
0
        public void NullableTypesSerializeToXsiNilWhenNull()
        {
            var message = new MessageWithNullable
            {
                FirstName    = "FirstName",
                LastName     = "LastName",
                EmailAddress = "EmailAddress",
                BirthDate    = null
            };

            using (var stream = new MemoryStream())
            {
                SerializerFactory.Create <MessageWithNullable>().Serialize(message, stream);
                stream.Position = 0;
                var reader = new StreamReader(stream);
                var xml    = reader.ReadToEnd();

                var expected = XDocument.Parse(@"<?xml version=""1.0""?>
<MessageWithNullable xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
   <FirstName>FirstName</FirstName>
   <LastName>LastName</LastName>
   <EmailAddress>EmailAddress</EmailAddress>
   <BirthDate xsi:nil=""true""></BirthDate>
</MessageWithNullable>
");
                var actual   = XDocument.Parse(xml);

                Assert.AreEqual(expected.ToString(), actual.ToString());
            }
        }
        public void NullableTypeSerializeToValueWhenNotNull()
        {
            var message = new MessageWithNullable
            {
                FirstName = "FirstName",
                LastName = "LastName",
                EmailAddress = "EmailAddress",
                BirthDate = new DateTime(1950, 04, 25)
            };

            using (var stream = new MemoryStream())
            {
                SerializerFactory.Create<MessageWithNullable>().Serialize(message, stream);
                stream.Position = 0;
                var reader = new StreamReader(stream);
                var xml = reader.ReadToEnd();

                var expected = XDocument.Parse(@"<?xml version=""1.0""?>
<MessageWithNullable xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
   <FirstName>FirstName</FirstName>
   <LastName>LastName</LastName>
   <EmailAddress>EmailAddress</EmailAddress>
   <BirthDate>1950-04-25T00:00:00</BirthDate>
</MessageWithNullable>
");
                var actual = XDocument.Parse(xml);

                Assert.AreEqual(expected.ToString(), actual.ToString());
            }
        }
Example #3
0
        public void NullableTypeSerializeToValueWhenNotNull()
        {
            var message = new MessageWithNullable
            {
                FirstName    = "FirstName",
                LastName     = "LastName",
                EmailAddress = "EmailAddress",
                BirthDate    = new DateTime(1950, 04, 25)
            };

            using (var stream = new MemoryStream())
            {
                SerializerFactory.Create <MessageWithNullable>().Serialize(message, stream);
                stream.Position = 0;
                var reader = new StreamReader(stream);
                var xml    = reader.ReadToEnd();

#if NET452
                var birthDate = "1950-04-25T00:00:00";
#else
                var birthDate = "1950-04-25T00:00:00.0000000";
#endif

                var expected = XDocument.Parse($@"<?xml version=""1.0""?>
<MessageWithNullable xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
   <FirstName>FirstName</FirstName>
   <LastName>LastName</LastName>
   <EmailAddress>EmailAddress</EmailAddress>
   <BirthDate>{birthDate}</BirthDate>
</MessageWithNullable>
");
                var actual   = XDocument.Parse(xml);

                Assert.AreEqual(expected.ToString(), actual.ToString());
            }
        }