public static string XmlSerializeToString <T>(T serializedObject, Encoding encoding = null) where T : class { if (serializedObject == null) { return(""); } XmlDocument xmlDocument = new XmlDocument(); StringBuilder sb = new StringBuilder(); using (TextWriter textWriter = new UserEncodingStringWriter(sb, encoding ?? new UTF8Encoding(false))) { using (XmlTextWriter xmlTextWriter = new XmlTextWriter(textWriter)) { xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.IndentChar = ' '; xmlTextWriter.Indentation = 4; xmlTextWriter.Namespaces = false; SimpleXmlSerializer serializer = new SimpleXmlSerializer(false, xmlDocument, xmlDocument.DocumentElement); SimpleXmlSerializerBase.InvokeSerializationMethod(serializedObject, serializer); xmlDocument.WriteContentTo(xmlTextWriter); } } return(sb.ToString()); }
public override void ProcessCollection <T>( ICollection <T> collection, Func <SimpleXmlSerializerBase, T> createItemFunc = null) { if (IsDeserializing) { XmlElement startElement = (XmlElement)CurrentXmlElement.ParentNode; XmlElement prevElement; do { prevElement = CurrentXmlElement; SimpleXmlSerializerBase clonedSerializer = Clone(); T value = createItemFunc?.Invoke(clonedSerializer) ?? InvokeSerializationMethod <T>(null, clonedSerializer); CurrentXmlElement = clonedSerializer.CurrentXmlElement; collection.Add(value); if (clonedSerializer.CurrentXmlElement == startElement) { break; } } while (prevElement != CurrentXmlElement || CurrentXmlElement.NextSibling != null); } else { foreach (T value in collection) { CloneSerializerAndInvokeSerializationMethod(value); } } }
public virtual T CreateByKnownInheritors <T>(string name, SimpleXmlSerializerBase serializer = null) where T : class { IEnumerable <Type> knownInheritors = Attribute .GetCustomAttributes(typeof(T), typeof(KnownInheritorsAttribute)) .Cast <KnownInheritorsAttribute>() .SelectMany(attribute => attribute.InheritorTypes) .Distinct(); bool isEmpty = true; foreach (Type type in knownInheritors) { isEmpty = false; if (GetXmlRootName(type) == name) { serializer = serializer ?? Clone(); T value = (T)InvokeSerializationMethod(type, null, serializer); return(value); } } if (isEmpty) { throw new InvalidOperationException($"Type {typeof(T)} has no {nameof(KnownInheritorsAttribute)} attached"); } throw new NotSupportedException($"Unknown element name {name}"); }
public static string GenerateXmlSchemaString <T>(T serializedObject) where T : class { if (serializedObject == null) { return(""); } XmlDocument xmlDocument = new XmlDocument(); StringBuilder sb = new StringBuilder(); using (TextWriter textWriter = new StringWriter(sb)) { using (XmlTextWriter xmlTextWriter = new XmlTextWriter(textWriter)) { xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.IndentChar = ' '; xmlTextWriter.Indentation = 4; const string xmlNamespace = "xs"; XmlElement schemaElement = xmlDocument.CreateElement(xmlNamespace, "schema", "http://www.w3.org/2001/XMLSchema"); schemaElement.SetAttribute("elementFormDefault", "qualified"); schemaElement.SetAttribute("attributeFormDefault", "unqualified"); xmlDocument.InsertBefore(schemaElement, null); SchemaGeneratorSimpleXmlSerializer serializer = new SchemaGeneratorSimpleXmlSerializer(xmlDocument, schemaElement); SimpleXmlSerializerBase.InvokeSerializationMethod(serializedObject, serializer); serializer.PostProcess(); xmlDocument.WriteContentTo(xmlTextWriter); string unformatted = sb.ToString(); sb.Length = 0; xmlDocument.LoadXml(unformatted); xmlDocument.WriteContentTo(xmlTextWriter); } } return(sb.ToString()); }
public static T XmlDeserializeFromString <T>(string objectData) where T : class { XmlDocument xmlDocument = new XmlDocument(); T result; using (TextReader textReader = new StringReader(objectData)) { XmlReaderSettings xmlReaderSettings = new XmlReaderSettings { IgnoreWhitespace = true, IgnoreComments = true }; XmlReader xmlReader = XmlReader.Create(textReader, xmlReaderSettings); while (xmlReader.NodeType != XmlNodeType.Element) { xmlReader.Read(); } xmlDocument.Load(xmlReader); result = SimpleXmlSerializerBase.InvokeSerializationMethod <T>(null, new SimpleXmlSerializer(true, xmlDocument, xmlDocument.DocumentElement)); } return(result); }
public static object InvokeSerializationMethod(Type serializedObjectType, object serializedObject, SimpleXmlSerializerBase serializer) { MethodInfo serializationMethod = SerializationMethodProvider.GetSerializationMethod(serializedObjectType, serializedObject); return(serializationMethod.Invoke(null, new[] { serializedObject, serializer })); }
public static T InvokeSerializationMethod <T>(T serializedObject, SimpleXmlSerializerBase serializer) { return((T)InvokeSerializationMethod(typeof(T), serializedObject, serializer)); }