示例#1
0
        public void MetadataString_SerializeNewObjectWithEvilStringDeserializeOldClass_ProducesOldObjectWithBinHexString()
        {
            var newObject = new TestClassForXmlSerialization()
            {
                IsReallyCool = true,
                Name         = EvilString
            };

            var xmlString = string.Empty;

            using (var stringWriter = new StringWriter())
            {
                var serializer = new XmlSerializer(typeof(TestClassForXmlSerialization));
                serializer.Serialize(stringWriter, newObject);
                xmlString = stringWriter.ToString();
            }

            OldTestClassForXmlSerialization oldObject = null;

            using (var stringReader = new StringReader(xmlString))
            {
                var serializer = new XmlSerializer(typeof(OldTestClassForXmlSerialization));
                oldObject = serializer.Deserialize(stringReader) as OldTestClassForXmlSerialization;
            }

            Assert.Equal(newObject.IsReallyCool, oldObject.IsReallyCool);
            Assert.Equal(newObject.XmlString.XmlText, oldObject.Name);
        }
示例#2
0
        public void MetadataString_SerializeNewClassDeserializeOldClassUsingGoodString_ProducesEquivalentObject()
        {
            var newObject = new TestClassForXmlSerialization()
            {
                IsReallyCool = true,
                Name         = "Well isn't <that> special!"
            };

            var xmlString = string.Empty;

            using (var stringWriter = new StringWriter())
            {
                var serializer = new XmlSerializer(typeof(TestClassForXmlSerialization));
                serializer.Serialize(stringWriter, newObject);
                xmlString = stringWriter.ToString();
            }

            OldTestClassForXmlSerialization oldObject = null;

            using (var stringReader = new StringReader(xmlString))
            {
                var serializer = new XmlSerializer(typeof(OldTestClassForXmlSerialization));
                oldObject = serializer.Deserialize(stringReader) as OldTestClassForXmlSerialization;
            }

            Assert.Equal(newObject.IsReallyCool, oldObject.IsReallyCool);
            Assert.Equal(newObject.Name, oldObject.Name);
        }
示例#3
0
        public void MetadataString_SerializeOldObjectWithEvilString_ThrowsInvalidOperationOnDeserialize()
        {
            var oldEvilObject = new OldTestClassForXmlSerialization()
            {
                IsReallyCool = true,
                Name         = EvilString
            };

            var xmlString = string.Empty;

            using (var stringWriter = new StringWriter())
            {
                var serializer = new XmlSerializer(typeof(OldTestClassForXmlSerialization));
                serializer.Serialize(stringWriter, oldEvilObject);
                xmlString = stringWriter.ToString();
            }

            using (var stringReader = new StringReader(xmlString))
            {
                var serializer = new XmlSerializer(typeof(OldTestClassForXmlSerialization));
                Assert.Throws <System.InvalidOperationException>(() => serializer.Deserialize(stringReader));
            }
        }