public override object Deserialize(XmlTextReader r, Type type) { var col = type.Assembly.CreateInstance(type.FullName); var instanceName = r.Name; if (r.IsEmptyElement) { return(col); } var addToColl = type.GetMethod("Add"); r.Read(); // Loop until end of collection while (!(r.NodeType == XmlNodeType.EndElement && r.Name == instanceName)) { // Skip the end element node if (r.NodeType == XmlNodeType.EndElement) { r.Read(); } // Skip over white space or comments while (r.NodeType == XmlNodeType.Whitespace || r.NodeType == XmlNodeType.Comment) { r.Read(); } if (r.NodeType != XmlNodeType.Element) { continue; } var childType = addToColl.GetParameters()[0].ParameterType; object child = CustomSerializer.DeserializeObject(r, childType); object[] args = { child }; addToColl.Invoke(col, args); // break the endless loop if the object is an 'empty' object. if (r.IsEmptyElement) { r.Read(); } //else if (r.NodeType != XmlNodeType.EndElement) //{ // // Only an Element or EndElement is valid so throw an exception // //throw new FrameworkException( // //string.Format("CollectionSerializer expects all Xml nodes but found {0} node: {1}", // //r.NodeType.ToString(), r.Value)); //} } return(col); }
public void XmlAttributeDeserializesIntoProperty() { var xml = @"<AttributeContainer SomeValue=""abc""></AttributeContainer>"; var serializer = new CustomSerializer<AttributeContainer>(null, TestXmlSerializerOptions.Empty); var container = (AttributeContainer)serializer.DeserializeObject(xml); Assert.That(container.SomeValue, Is.EqualTo("abc")); }
public void XmlAttributeDeserializesIntoProperty() { var xml = @"<AttributeContainer SomeValue=""abc""></AttributeContainer>"; var serializer = new CustomSerializer <AttributeContainer>(null, TestXmlSerializerOptions.Empty); var container = (AttributeContainer)serializer.DeserializeObject(xml); Assert.That(container.SomeValue, Is.EqualTo("abc")); }
public void EnumAttributeDeserializesCorrectlyWhenExtraTypesArePassedIn() { var xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <EnumAttributeContainer xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" MyEnum=""Value2"" />"; var serializer = new CustomSerializer <EnumAttributeContainer>(null, TestXmlSerializerOptions.WithExtraTypes(typeof(IFoo))); var container = (EnumAttributeContainer)serializer.DeserializeObject(xml); Assert.That(container.MyEnum, Is.EqualTo(MyEnum.Value2)); }
public void EnumAttributeDeserializesCorrectlyWhenExtraTypesArePassedIn() { var xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <EnumAttributeContainer xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" MyEnum=""Value2"" />"; var serializer = new CustomSerializer<EnumAttributeContainer>(null, TestXmlSerializerOptions.WithExtraTypes(typeof(IFoo))); var container = (EnumAttributeContainer)serializer.DeserializeObject(xml); Assert.That(container.MyEnum, Is.EqualTo(MyEnum.Value2)); }
public void EnumAttributeDeserializesCorrectlyWhenIgnoreCaseForEnumIsPassedIn() { var xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <EnumAttributeContainer xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" MyEnum=""vALUE1"" />"; var serializer = new CustomSerializer <EnumAttributeContainer>(null, TestXmlSerializerOptions.WithExtraTypes()); var container = (EnumAttributeContainer)serializer.DeserializeObject(xml, new TestSerializeOptions { ShouldIgnoreCaseForEnum = true }); Assert.That(container.MyEnum, Is.EqualTo(MyEnum.Value1)); }
public void EnumElementDeserializesCorrectlyWhenIgnoreCaseForEnumIsPassedIn() { var xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <EnumElementContainer xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""> <MyEnum>vALUE1</MyEnum> </EnumElementContainer>"; var serializer = new CustomSerializer<EnumElementContainer>(null, TestXmlSerializerOptions.WithExtraTypes()); var container = (EnumElementContainer)serializer.DeserializeObject(xml, new TestSerializeOptions { ShouldIgnoreCaseForEnum = true }); Assert.That(container.MyEnum, Is.EqualTo(MyEnum.Value1)); }