/// <summary> /// Optimizes the xml element. /// </summary> /// <param name="element">The element.</param> /// <param name="optimalizationMode">The optimalization mode.</param> protected virtual void OptimizeXElement(XElement element, XmlSerializerOptimalizationMode optimalizationMode) { if (optimalizationMode == XmlSerializerOptimalizationMode.Performance) { return; } var agressive = (optimalizationMode == XmlSerializerOptimalizationMode.PrettyXmlAgressive); if (agressive) { // Important: children first foreach (var child in element.Elements()) { OptimizeXElement(child, optimalizationMode); } } var rootNamespaceAttributes = (from attribute in element.Attributes() where attribute.IsNamespaceDeclaration select attribute).ToList(); foreach (var rootNamespaceAttribute in rootNamespaceAttributes) { rootNamespaceAttribute.Remove(); } if (agressive) { // Clear xmlns namespaces if (!string.IsNullOrEmpty(element.Name.NamespaceName)) { element.Name = element.Name.LocalName; } } }
public void RespectsTheXmlAttributeAttributeOnRootElements(XmlSerializerOptimalizationMode mode) { var family = new XmlFamily(); family.LastName = "van Horrik"; family.Persons.Add(new XmlPerson { FirstName = "Geert", LastName = family.LastName, Gender = Gender.Male }); var newFamily = SerializationTestHelper.SerializeAndDeserialize(family, SerializationTestHelper.GetXmlSerializer(), new XmlSerializationConfiguration { OptimalizationMode = mode }); Assert.AreEqual(family.LastName, newFamily.LastName); Assert.AreEqual(1, newFamily.Persons.Count); var newPerson = newFamily.Persons.First(); Assert.AreEqual(family.Persons[0].FirstName, newPerson.FirstName); Assert.AreEqual(family.Persons[0].LastName, newPerson.LastName); Assert.AreEqual(family.Persons[0].Gender, newPerson.Gender); }
public static IXmlSerializer GetXmlSerializer(XmlSerializerOptimalizationMode optimalizationMode, ISerializationManager serializationManager = null) { if (serializationManager == null) { serializationManager = new SerializationManager(); } var serializer = TypeFactory.Default.CreateInstanceWithParametersAndAutoCompletion <XmlSerializer>(serializationManager); serializer.OptimalizationMode = optimalizationMode; return(serializer); }
public void SerializesModelsWithOnlyAttributes(XmlSerializerOptimalizationMode mode) { var family = new XmlFamily(); family.LastName = "van Horrik"; family.ModelsWithAttributesOnly.Add(new XmlModelWithAttributesOnly { FirstName = "Geert", }); var newFamily = SerializationTestHelper.SerializeAndDeserialize(family, SerializationTestHelper.GetXmlSerializer(mode)); Assert.AreEqual(family.LastName, newFamily.LastName); Assert.AreEqual(1, newFamily.ModelsWithAttributesOnly.Count); var newModelWithAttributesOnly = newFamily.ModelsWithAttributesOnly.First(); Assert.AreEqual(family.ModelsWithAttributesOnly[0].FirstName, newModelWithAttributesOnly.FirstName); }
public void SerializesModelsWithOnlyAttributes(XmlSerializerOptimalizationMode mode) { var family = new XmlFamily(); family.LastName = "van Horrik"; family.ModelsWithAttributesOnly.Add(new XmlModelWithAttributesOnly { FirstName = "Geert", }); var newFamily = SerializationTestHelper.SerializeAndDeserialize(family, SerializationTestHelper.GetXmlSerializer(), new XmlSerializationConfiguration { // No longer using optimization mode, but keep this test alive }); Assert.AreEqual(family.LastName, newFamily.LastName); Assert.AreEqual(1, newFamily.ModelsWithAttributesOnly.Count); var newModelWithAttributesOnly = newFamily.ModelsWithAttributesOnly.First(); Assert.AreEqual(family.ModelsWithAttributesOnly[0].FirstName, newModelWithAttributesOnly.FirstName); }
public void SerializesModelsWithOnlyAttributes(XmlSerializerOptimalizationMode mode) { var family = new XmlFamily(); family.LastName = "van Horrik"; family.ModelsWithAttributesOnly.Add(new XmlModelWithAttributesOnly { FirstName = "Geert", }); var newFamily = SerializationTestHelper.SerializeAndDeserialize(family, SerializationTestHelper.GetXmlSerializer(mode)); Assert.AreEqual(family.LastName, newFamily.LastName); Assert.AreEqual(1, newFamily.ModelsWithAttributesOnly.Count); var newModelWithAttributesOnly = newFamily.ModelsWithAttributesOnly.First(); Assert.AreEqual(family.ModelsWithAttributesOnly[0].FirstName, newModelWithAttributesOnly.FirstName); }
public void RespectsTheXmlAttributeAttributeOnRootElements(XmlSerializerOptimalizationMode mode) { var family = new XmlFamily(); family.LastName = "van Horrik"; family.Persons.Add(new XmlPerson { FirstName = "Geert", LastName = family.LastName, Gender = Gender.Male }); var newFamily = SerializationTestHelper.SerializeAndDeserialize(family, SerializationTestHelper.GetXmlSerializer(mode)); Assert.AreEqual(family.LastName, newFamily.LastName); Assert.AreEqual(1, newFamily.Persons.Count); var newPerson = newFamily.Persons.First(); Assert.AreEqual(family.Persons[0].FirstName, newPerson.FirstName); Assert.AreEqual(family.Persons[0].LastName, newPerson.LastName); Assert.AreEqual(family.Persons[0].Gender, newPerson.Gender); }