예제 #1
0
        /// <summary>
        /// Generic method to deserialize an XML element.
        /// </summary>
        /// <typeparam name="T">The type to deserialize to.</typeparam>
        /// <param name="element">The XML element to deserialize.</param>
        /// <returns>The deserialized object.</returns>
        public static T Deserialize <T>(this XmlElement element)
        {
            XmlReader        reader   = element.CreateNavigator().ReadSubtree();
            XmlRootAttribute rootAttr = new XmlRootAttribute(element.LocalName);

            rootAttr.Namespace = element.NamespaceURI;
            //XmlSerializer xs = new XmlSerializer(typeof(T), rootAttr);  -- Use new CachingXmlSerializerFactory class
            var    xs = CachingXmlSerializerFactory.Create(typeof(T), rootAttr);
            object rv = xs.Deserialize(reader);

            return((T)rv);
        }
예제 #2
0
        /// <summary>
        /// Generic method to deserialize an XML document.
        /// </summary>
        /// <typeparam name="T">The type to deserialize to.</typeparam>
        /// <param name="document">The XML document to deserialize.</param>
        /// <returns>The deserialized object.</returns>
        public static T Deserialize <T>(this XmlDocument document)
        {
            if (document.DocumentElement == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidDocumentAttribute);
            }

            XmlReader reader = document.CreateNavigator().ReadSubtree();
            // ReSharper disable PossibleNullReferenceException
            XmlRootAttribute rootAttr = new XmlRootAttribute(document.DocumentElement.LocalName);

            // ReSharper restore PossibleNullReferenceException
            rootAttr.Namespace = document.DocumentElement.NamespaceURI;
            //XmlSerializer xs = new XmlSerializer(typeof(T), rootAttr); -- Use new CachingXmlSerializerFactory class
            var    xs = CachingXmlSerializerFactory.Create(typeof(T), rootAttr);
            object rv = xs.Deserialize(reader);

            return((T)rv);
        }
예제 #3
0
        /// <summary>
        /// Serializes any object to XML.
        /// </summary>
        /// <param name="report">The object to serialize.</param>
        /// <returns>Xml of the serialized object.</returns>
        public static XmlDocument SerializeToXml(this Object report)
        {
            // Get XmlTypeAttribute
            XmlTypeAttribute xta = Attribute.GetCustomAttribute(report.GetType(), typeof(XmlTypeAttribute)) as XmlTypeAttribute;

            if (xta == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidDocumentAttribute);
            }

            // Get XmlRootAttribute
            XmlRootAttribute xra = Attribute.GetCustomAttribute(report.GetType(), typeof(XmlRootAttribute)) as XmlRootAttribute;

            if (xra == null)
            {
                xra = new XmlRootAttribute();
            }
            xra.Namespace = xta.Namespace;

            XmlDocument output = new XmlDocument();

            output.PreserveWhitespace = true;

            XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();

            xsn.Add(string.Empty, string.Empty);

            StringBuilder sb   = new StringBuilder();
            XmlWriter     xmlw = XmlWriter.Create(sb);

            //XmlSerializer xmlSerializer = new XmlSerializer(report.GetType(), xra); -- Use new CachingXmlSerializerFactory class
            var xmlSerializer = CachingXmlSerializerFactory.Create(report.GetType(), xra);

            xmlSerializer.Serialize(xmlw, report, xsn);
            xmlw.Close();

            output.LoadXml(sb.ToString());

            return(output);
        }