Ejemplo n.º 1
0
        public void Load(System.Xml.XmlTextReader tr, string localName)
        {
            tr.ReadStartElement(localName);

            tr.ReadElementContentAsBoolean("IsEnabled", string.Empty);

            tr.ReadEndElement();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads the element content as collection.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public static System.Collections.Generic.List <T> ReadElementContentAsCollection <T>(this System.Xml.XmlReader reader) where T : class
        {
            System.Collections.Generic.List <T>    result     = new System.Collections.Generic.List <T>();
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            string xml = reader.ReadOuterXml();

            using (System.IO.StringReader sr = new System.IO.StringReader(xml))
            {
                using (System.Xml.XmlTextReader xmlTextReader = new System.Xml.XmlTextReader(sr))
                {
                    xmlTextReader.ReadStartElement();
                    while (!xmlTextReader.EOF)
                    {
                        if (xmlTextReader.NodeType == System.Xml.XmlNodeType.EndElement)
                        {
                            xmlTextReader.ReadEndElement();
                            continue;
                        }

                        T obj;

                        if (xmlTextReader.IsEmptyElement && xmlTextReader.HasAttributes)
                        {
                            obj = serializer.Deserialize(xmlTextReader) as T;
                        }
                        else
                        {
                            System.Xml.XmlReader subTree = xmlTextReader.ReadSubtree();
                            obj = serializer.Deserialize(subTree) as T;
                        }
                        if (obj != null)
                        {
                            result.Add(obj);
                        }

                        if (!xmlTextReader.IsEmptyElement)
                        {
                            xmlTextReader.Read();
                        }
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// deserialize xml document to name and description and optionally fill stream with content
        /// </summary>
        /// <param name="xml">xml containing data</param>
        /// <param name="name">Name attribute in xml</param>
        /// <param name="description">description attribute</param>
        /// <param name="outputStream">null if no content is not required</param>
        public static void XmlToTempFile(string xml, string tmpFilePath, out string name, out string description, out FluidTrade.Reporting.Interfaces.IStaticReportTranslation translator)
        {
            Stream outputStream = null;

            if (string.IsNullOrEmpty(tmpFilePath) == false)
            {
                outputStream = File.Open(tmpFilePath, FileMode.CreateNew);
            }
            name        = null;
            description = null;
            string base64File = null;
            string base64TranslationContent = null;

            translator = null;
            //create the xmlReader to walk the xml doc
            using (StringReader stringReader = new StringReader(xml))
            {
                using (System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stringReader))
                {
                    xmlReader.WhitespaceHandling = System.Xml.WhitespaceHandling.Significant;
                    //read to get to the first node
                    xmlReader.Read();

                    //get the name and description attributes
                    name        = xmlReader.GetAttribute(Name);
                    description = xmlReader.GetAttribute(Description);


                    if (outputStream == null)
                    {
                        return;                        //dont need to do anymore
                    }
                    //get to the content
                    xmlReader.Read();
                    base64File = xmlReader.ReadElementContentAsString();

                    //get to the translator
                    if (xmlReader.NodeType != System.Xml.XmlNodeType.EndElement)
                    {
                        xmlReader.Read();
                        base64TranslationContent = xmlReader.ReadContentAsString();
                    }
                    xmlReader.ReadEndElement();
                }
            }

            //if no outputStream or no content in xml then nothing left to do
            if (outputStream == null || string.IsNullOrEmpty(base64File))
            {
                return;
            }

            //convert the content to bytes
            Byte[] bytes = Convert.FromBase64String(base64File);

            //write the bytes to the stream
            using (BinaryWriter binWriter = new BinaryWriter(outputStream))
            {
                binWriter.Write(bytes);
                binWriter.Flush();
            }


            if (string.IsNullOrEmpty(base64TranslationContent) == false)
            {
                //convert the content to bytes
                Byte[] translationBytes = Convert.FromBase64String(base64TranslationContent);

                System.Reflection.Assembly translationAssembly = AppDomain.CurrentDomain.Load(translationBytes);
                Type[] types = translationAssembly.GetTypes();
                foreach (Type t in types)
                {
                    if (t.GetInterface("FluidTrade.Reporting.Interfaces.IStaticReportTranslation") != null)
                    {
                        translator = Activator.CreateInstance(t) as FluidTrade.Reporting.Interfaces.IStaticReportTranslation;
                        break;
                    }
                }
            }
        }