Example #1
0
 public static object Deserialize(this XContainer element, Type type, XmlSerializer serializer)
 {
     using (var reader = element.CreateReader())
     {
         return((serializer ?? new XmlSerializer(type)).Deserialize(reader));
     }
 }
                //[Variation(Priority = 0, Desc = "Annotation on document - Omit", Params = new object[] { typeof(XDocument), "Simple.xml", SaveOptions.OmitDuplicateNamespaces })]
                //[Variation(Priority = 1, Desc = "Annotation on document - Omit + Disable", Params = new object[] { typeof(XDocument), "Simple.xml", SaveOptions.OmitDuplicateNamespaces | SaveOptions.DisableFormatting })]
                //[Variation(Priority = 0, Desc = "Annotation on element - Omit", Params = new object[] { typeof(XElement), "Simple.xml", SaveOptions.OmitDuplicateNamespaces })]
                //[Variation(Priority = 1, Desc = "Annotation on element - Omit + Disable", Params = new object[] { typeof(XElement), "Simple.xml", SaveOptions.OmitDuplicateNamespaces | SaveOptions.DisableFormatting })]
                public void XDocAnnotation()
                {
                    Type        t        = CurrentChild.Params[0] as Type;
                    string      fileName = Path.Combine(s_MyPath, CurrentChild.Params[1] as string);
                    SaveOptions so       = (SaveOptions)CurrentChild.Params[2];

                    using (MemoryStream ms = new MemoryStream())
                    {
                        XDocument toClean = XDocument.Load(FilePathUtil.getStream(fileName));
                        using (XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings()
                        {
                            OmitXmlDeclaration = true, NamespaceHandling = NamespaceHandling.OmitDuplicates
                        }))
                        {
                            toClean.Save(w);
                        }
                        ms.Position = 0;

                        using (XmlReader r1 = XmlReader.Create(ms, new XmlReaderSettings()
                        {
                            IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Ignore
                        }))
                        {
                            XContainer doc = GetContainer(fileName, t);
                            doc.AddAnnotation(so);
                            using (XmlReader r2 = doc.CreateReader())
                            {
                                ReaderDiff.Compare(r1, r2);
                            }
                        }
                    }
                }
 public static T Deserialize <T>(this XContainer element, XmlSerializer serializer)
 {
     using (var reader = element.CreateReader())
     {
         object result = (serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
         return((T)result);
     }
 }
Example #4
0
 public static T Deserialize <T>(this XContainer xml)
 {
     if (xml == null)
     {
         return(default(T));
     }
     return(xml.CreateReader().Deserialize <T>());
 }
 public static XmlNode GetXmlNode(this XContainer element)
 {
     using (var xmlReader = element.CreateReader())
     {
         var xmlDoc = new XmlDocument();
         xmlDoc.Load(xmlReader);
         return(xmlDoc.DocumentElement);
     }
 }
Example #6
0
 public static T Deserialize <T>(this XContainer element, XmlSerializer serializer = null)
 {
     if (element == null)
     {
         throw new ArgumentNullException();
     }
     using (var reader = element.CreateReader())
         return((T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader));
 }
Example #7
0
        ///// <summary>
        ///// Converts from an XElement to an XmlElement
        ///// </summary>
        ///// <param name="xElement"></param>
        ///// <returns></returns>
        public static XmlNode ToXmlElement(this XContainer xElement)
        {
            var xmlDocument = new XmlDocument();

            using (var xmlReader = xElement.CreateReader())
            {
                xmlDocument.Load(xmlReader);
            }
            return(xmlDocument.DocumentElement);
        }
Example #8
0
 public static XmlDocument ToXmlDocument(this XContainer valor)
 {
     using (var leitor = valor.CreateReader())
     {
         var xml = new XmlDocument();
         xml.PreserveWhitespace = false;
         xml.Load(leitor);
         return(xml);
     }
 }
Example #9
0
        public override XmlReader ReadSubtree()
        {
            XContainer c = _source as XContainer;

            if (c == null)
            {
                throw new InvalidOperationException(SR.Format(SR.InvalidOperation_BadNodeType, NodeType));
            }
            return(c.CreateReader());
        }
Example #10
0
 public static T ToObject <T>(this XContainer doc, XmlSerializer serializer /* = null */)
 {
     if (doc == null)
     {
         throw new ArgumentNullException();
     }
     using (var reader = doc.CreateReader())
     {
         return((T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader));
     }
 }
Example #11
0
 public static T ToContractObject <T>(this XContainer doc, DataContractSerializer serializer = null)
 {
     if (doc == null)
     {
         throw new ArgumentNullException();
     }
     using (var reader = doc.CreateReader())
     {
         return((T)(serializer ?? new DataContractSerializer(typeof(T))).ReadObject(reader));
     }
 }
Example #12
0
 public static T Deserialize <T>(this XContainer element, XmlSerializer serializer)
 {
     using (var reader = element.CreateReader())
     {
         object result = serializer.Deserialize(reader);
         if (result is T)
         {
             return((T)result);
         }
     }
     return(default(T));
 }
 public static T DeserializeXML <T>(this XContainer element)
 {
     using (var reader = element.CreateReader())
     {
         var result = new XmlSerializer(typeof(T)).Deserialize(reader);
         if (result is T)
         {
             return((T)result);
         }
     }
     return(default(T));
 }
        public static dynamic Deserialize <T>(this XContainer element, XmlSerializer serializer)
            where T : class
        {
            var type = typeof(T);

            if (type.IsGenericType &&
                type.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)))
            {
                // get type of a list element
                var elementType = type.GetGenericArguments()[0];

                // add new item to new list
                using (var reader = element.CreateReader())
                {
                    serializer = serializer ?? new XmlSerializer(elementType);
                    var result = serializer.Deserialize(reader);
                    if (result.GetType().IsAssignableFrom(elementType))
                    {
                        return(Cast(result, elementType));
                    }
                }

                return(null);
            }

            // just create a new instance of T if T is not List
            using (var reader = element.CreateReader())
            {
                serializer = serializer ?? new XmlSerializer(typeof(T));
                object result = serializer.Deserialize(reader);
                if (result is T)
                {
                    return((T)result);
                }
            }

            return(default(T));
        }
Example #15
0
        /// <summary>
        ///     Deserializes the specified document to serialize.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="documentToSerialize">The document to serialize.</param>
        /// <returns>``0.</returns>
        /// <exception cref="System.ArgumentNullException">documentToSerialize</exception>
        public static T Deserialize <T>(XContainer documentToSerialize)
        {
            // pre-conditions
            if (documentToSerialize == null)
            {
                throw new ArgumentNullException("documentToSerialize");
            }

            var xs = new XmlSerializer(typeof(T));

            var xnr = documentToSerialize.CreateReader();

            return((T)xs.Deserialize(xnr));
        }
Example #16
0
 public static void Validate(this XContainer node, XmlReaderSettings settings)
 {
     if (node == null)
     {
         throw new ArgumentNullException();
     }
     using (var innerReader = node.CreateReader())
         using (var reader = XmlReader.Create(innerReader, settings))
         {
             while (reader.Read())
             {
                 ;
             }
         }
 }
        public static XDocument XslTransfrom(this XContainer document, string xslPath)
        {
            var transformedDoc = new XDocument();

            using (XmlWriter writer = transformedDoc.CreateWriter())
            {
                var xmlResolver = new XmlUrlResolver();
                var transform   = new XslCompiledTransform();
                transform.Load(xslPath, new XsltSettings {
                    EnableDocumentFunction = true
                }, xmlResolver);
                //transform.Load(XmlReader.Create(new StringReader(xslPath)));
                transform.Transform(document.CreateReader(), writer);
            }

            return(transformedDoc);
        }
                //[Variation(Priority = 0, Desc = "Multiple annotations on node - XDocument", Param = typeof(XDocument))]
                //[Variation(Priority = 0, Desc = "Multiple annotations on node - XElement", Param = typeof(XElement))]
                public void MultipleAnnotationsOnElement()
                {
                    Type       t   = CurrentChild.Param as Type;
                    string     xml = @"<A xmlns:p='a1'><B xmlns:q='a2'><C xmlns:p='a1'><D xmlns:q='a2' ><E xmlns:p='a1' /></D></C></B></A>";
                    XContainer reF = (t == typeof(XElement) ? XElement.Parse(xml) as XContainer : XDocument.Parse(xml) as XContainer);  // I want dynamics!!!

                    SaveOptions[] options = new SaveOptions[] { SaveOptions.None, SaveOptions.DisableFormatting, SaveOptions.OmitDuplicateNamespaces, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces };

                    foreach (SaveOptions[] opts in Tuples2(options))
                    {
                        XContainer gp = (t == typeof(XElement) ? XElement.Parse(xml) as XContainer : XDocument.Parse(xml) as XContainer);
                        foreach (SaveOptions o in opts)
                        {
                            gp.AddAnnotation(o);
                        }
                        TestLog.Compare(reF.ToString(opts[0]), gp.ToString(), "On root - ToString()");
                        ReaderDiffNSAware.CompareNamespaceAware(opts[0], reF.CreateReader(), gp.CreateReader());
                    }
                }
                //[Variation(Priority = 2, Desc = "Annotation on document without omit - None", Params = new object[] { typeof(XDocument), "Simple.xml", SaveOptions.None })]
                //[Variation(Priority = 2, Desc = "Annotation on document without omit - DisableFormating", Params = new object[] { typeof(XDocument), "Simple.xml", SaveOptions.DisableFormatting })]
                //[Variation(Priority = 2, Desc = "Annotation on element without omit - None", Params = new object[] { typeof(XElement), "Simple.xml", SaveOptions.None })]
                //[Variation(Priority = 2, Desc = "Annotation on element without omit - DisableFormating", Params = new object[] { typeof(XElement), "Simple.xml", SaveOptions.DisableFormatting })]
                public void AnnotationWithoutTheOmitDuplicates()
                {
                    Type        t        = CurrentChild.Params[0] as Type;
                    string      fileName = Path.Combine(s_MyPath, CurrentChild.Params[1] as string);
                    SaveOptions so       = (SaveOptions)CurrentChild.Params[2];

                    using (XmlReader r1 = XmlReader.Create(FilePathUtil.getStream(fileName), new XmlReaderSettings()
                    {
                        IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Ignore
                    }))
                    {
                        XContainer doc = GetContainer(fileName, t);
                        doc.AddAnnotation(so);
                        using (XmlReader r2 = doc.CreateReader())
                        {
                            ReaderDiff.Compare(r1, r2);
                        }
                    }
                }
                //[Variation(Priority = 0, Desc = "No annotation - element", Params = new object[] { typeof(XElement), "Simple.xml" })]
                //[Variation(Priority = 0, Desc = "No annotation - document", Params = new object[] { typeof(XDocument), "Simple.xml" })]
                public void NoAnnotation()
                {
                    Type   t        = CurrentChild.Params[0] as Type;
                    string fileName = Path.Combine(s_MyPath, CurrentChild.Params[1] as string);

                    // create normal reader for comparison
                    using (XmlReader r1 = XmlReader.Create(FilePathUtil.getStream(fileName), new XmlReaderSettings()
                    {
                        IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Ignore
                    }))
                    {
                        // create reader from Xlinq
                        XContainer c = GetContainer(fileName, t);
                        using (XmlReader r2 = c.CreateReader())
                        {
                            ReaderDiff.Compare(r1, r2);
                        }
                    }
                }
        protected static TSection DeserializeSection <TSection>(XContainer xml, string name)
            where TSection : ConfigurationSection, new()
        {
            var config = new TSection();
            const BindingFlags flags        = BindingFlags.Instance | BindingFlags.NonPublic;
            MethodInfo         deserializer = typeof(TSection).GetMethod(DeserializeSectionMethodName, flags);
            XContainer         sectionXml   = name.Split(PathSeparator).Aggregate(xml, (current, part) => current.Element(part));

            if (sectionXml == null)
            {
                return(null);
            }

            try
            {
                deserializer.Invoke(config, new object[] { sectionXml.CreateReader() });
            }
            catch (TargetInvocationException error)
            {
                throw error.InnerException;
            }

            return(config);
        }
Example #22
0
 public static Object ToXmlObject(this XContainer valor, Type tipo)
 {
     return(Deserializar(tipo, valor.CreateReader()));
 }