private static bool IsPropertySerializable(PropertyInfo property) { DesignerSerializationVisibilityAttribute attribute = WindowProfileSerializer.GetAttribute <DesignerSerializationVisibilityAttribute>((MemberInfo)property); if (attribute != null && attribute.Visibility == DesignerSerializationVisibility.Hidden || !property.CanRead) { return(false); } if (!property.CanWrite) { return(WindowProfileSerializer.IsSequenceType(property.PropertyType)); } return(true); }
public void Serialize(object element, Stream stream) { if (element == null) { throw new ArgumentNullException("element"); } if (WindowProfileSerializer.IsSequenceType(element.GetType())) { throw new ArgumentException("Root serialized element must not be a sequence type", "element"); } XmlWriterSettings settings = new XmlWriterSettings() { CheckCharacters = false, CloseOutput = false, Indent = false, NewLineOnAttributes = false, OmitXmlDeclaration = true, Encoding = Encoding.UTF8 }; using (XmlWriter writer = XmlWriter.Create(stream, settings)) this.SerializeInternal(element, writer, true); }
private void SerializeInternal(object element, XmlWriter writer, bool isRootElement) { if (element == null) { return; } ICustomXmlSerializer customXmlSerializer = (ICustomXmlSerializer)null; if (this.Mode == WindowProfileSerializerMode.Custom) { ICustomXmlSerializable customXmlSerializable = element as ICustomXmlSerializable; if (customXmlSerializable != null) { customXmlSerializer = customXmlSerializable.CreateSerializer(); if (customXmlSerializer == null) { return; } } } Type type = element.GetType(); if (this.Mode == WindowProfileSerializerMode.Reflection && WindowProfileSerializer.IsTypeNonSerializable(type)) { return; } if (WindowProfileSerializer.IsSequenceType(type)) { this.SerializeSequence(element as IEnumerable, writer); } else { if (this.GetPrefix(type) != null) { writer.WriteStartElement(this.GetPrefix(type), type.Name, this.GetClrNamespace(type)); } else { writer.WriteStartElement(type.Name, this.GetClrNamespace(type)); } if (isRootElement) { this.WriteNamespaceDeclarations(writer); } object contentPropertyValue = (object)null; IEnumerable <KeyValuePair <string, object> > enumerable; if (customXmlSerializer != null) { customXmlSerializer.WriteXmlAttributes(writer); enumerable = customXmlSerializer.GetNonContentPropertyValues(); contentPropertyValue = customXmlSerializer.Content; } else { enumerable = (IEnumerable <KeyValuePair <string, object> >)WindowProfileSerializer.GetChildPropertiesAndContent(element, writer, type, ref contentPropertyValue); } foreach (KeyValuePair <string, object> keyValuePair in enumerable) { string localName = type.Name + "." + keyValuePair.Key; if (this.GetPrefix(type) != null) { writer.WriteStartElement(this.GetPrefix(type), localName, this.GetClrNamespace(type)); } else { writer.WriteStartElement(localName, this.GetClrNamespace(type)); } this.SerializeInternal(keyValuePair.Value, writer, false); writer.WriteEndElement(); } if (contentPropertyValue != null) { this.SerializeInternal(contentPropertyValue, writer, false); } writer.WriteEndElement(); } }