Created
March 22, 2016 20:28
-
-
Save pmunin/2059b8dbd65a130483b0 to your computer and use it in GitHub Desktop.
.NET Xml Serialization Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Xml; | |
| using System.Xml.Linq; | |
| using System.Xml.Serialization; | |
| public static class XNodeSerializationExtensions | |
| { | |
| static string GetXmlRootName(XNode xml) | |
| { | |
| return (xml as XDocument)?.Root?.Name.LocalName | |
| ?? (xml as XElement)?.Name.LocalName; | |
| } | |
| public static T Deserialize<T>(this XNode xml, XmlSerializer serializer = null, XmlDeserializationEvents? events = null) | |
| { | |
| //We need XmlRootAttribute. Why? See here http://stackoverflow.com/a/4761975/508797 | |
| var rootName = GetXmlRootName(xml); | |
| var deserializer = serializer ?? | |
| (rootName != null | |
| ? new XmlSerializer(typeof(T), new XmlRootAttribute(rootName)) | |
| : new XmlSerializer(typeof(T)) | |
| ) | |
| ; | |
| using (var rdr = xml.CreateReader()) | |
| { | |
| if (events != null) | |
| return (T)deserializer.Deserialize(rdr, events.Value); | |
| else | |
| return (T)deserializer.Deserialize(rdr); | |
| } | |
| } | |
| public static T TryDeserialize<T>(this XNode xml, XmlSerializer serializer = null, XmlDeserializationEvents? events = null, Func<XNode, T> ifFails = null) | |
| { | |
| var rootName = GetXmlRootName(xml); | |
| var deserializer = serializer ?? | |
| (rootName != null | |
| ? new XmlSerializer(typeof(T), new XmlRootAttribute(rootName)) | |
| : new XmlSerializer(typeof(T)) | |
| ) | |
| ; | |
| using (var rdr = xml.CreateReader()) | |
| { | |
| if (!deserializer.CanDeserialize(rdr)) | |
| return ifFails == null ? default(T) : ifFails(xml); | |
| if (events != null) | |
| return (T)deserializer.Deserialize(rdr, events.Value); | |
| else | |
| return (T)deserializer.Deserialize(rdr); | |
| } | |
| } | |
| static XDocument Serialize<T>(T obj, XmlSerializer serializer = null, bool omitNamespaces = true) | |
| { | |
| serializer = serializer ?? new XmlSerializer(typeof(T)); | |
| var newXml = new XDocument(); | |
| using (var xml = newXml.CreateWriter()) | |
| { | |
| serializer.Serialize(xml, obj, omitNamespaces ? new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }) : null); | |
| } | |
| return newXml; | |
| } | |
| /// <summary> | |
| /// Serializes object to Xml and replace current xml node with new generated xml | |
| /// </summary> | |
| /// <typeparam name="TXNode"></typeparam> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="xml"></param> | |
| /// <param name="obj"></param> | |
| /// <param name="serializer"></param> | |
| /// <param name="omitNamespaces"></param> | |
| /// <returns></returns> | |
| public static TXNode Serialize<TXNode, T>(this TXNode xml, T obj, XmlSerializer serializer = null) where TXNode : XNode | |
| { | |
| var newXmlDoc = Serialize(obj, serializer); | |
| var newElement = newXmlDoc.Root; | |
| if (!(xml is XDocument)) | |
| { | |
| xml.ReplaceWith(newElement); | |
| return newElement as TXNode; | |
| } | |
| var xmlDoc = xml as XDocument; | |
| if (xmlDoc == null) | |
| throw new NotSupportedException("Unexpected XNode type - expected XDocument"); | |
| xmlDoc.RemoveNodes();//For XDocument with existing content writing works only if RemoveNodes was executed prior | |
| using (var wr = xmlDoc.CreateWriter()) | |
| { | |
| newXmlDoc.Root.WriteTo(wr); | |
| return xml; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment