示例#1
0
        /// <summary>
        /// Process XML document
        /// </summary>
        /// <typeparam name="T">The type to deserialize from the document</typeparam>
        /// <param name="transform">The XSL transformation to apply. May be <c>null</c>.</param>
        /// <param name="doc">The XML document to deserialize from.</param>
        /// <returns>The result of the deserialization.</returns>
        private static APIResult <T> DeserializeAPIResultCore <T>(XslCompiledTransform transform, XmlDocument doc)
        {
            APIResult <T> result = null;

            try
            {
                // Deserialization with a transform
                using (XmlNodeReader reader = new XmlNodeReader(doc))
                {
                    if (transform != null)
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                            {
                                // Apply the XSL transform
                                writer.Formatting = Formatting.Indented;
                                transform.Transform(reader, writer);
                                writer.Flush();

                                // Deserialize from the given stream
                                stream.Seek(0, SeekOrigin.Begin);
                                XmlSerializer xs = new XmlSerializer(typeof(APIResult <T>));
                                result = (APIResult <T>)xs.Deserialize(stream);
                            }
                        }
                    }
                    // Deserialization without transform
                    else
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(APIResult <T>));
                        result = (APIResult <T>)xs.Deserialize(reader);
                    }
                }

                // Fix times
                DateTime requestTime = DateTime.Now.ToUniversalTime();
                double   CCPOffset   = (result.CurrentTime - requestTime).TotalMilliseconds;
                result.SynchronizeWithLocalClock(CCPOffset);
            }
            // An error occurred during the XSL transform
            catch (XsltException exc)
            {
                ExceptionHandler.LogException(exc, true);
                result = new APIResult <T>(exc);
            }
            // An error occured during the deserialization
            catch (InvalidOperationException exc)
            {
                ExceptionHandler.LogException(exc, true);
                result = new APIResult <T>(exc);
            }
            catch (XmlException exc)
            {
                ExceptionHandler.LogException(exc, true);
                result = new APIResult <T>(exc);
            }

            // Stores XMLDocument
            result.XmlDocument = doc;
            return(result);
        }