///<summary>For late binding of class type.</summary>
        public static string Serialize(Type classType, object obj)
        {
            StringBuilder strBuild = new StringBuilder();

                        #if DEBUG
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent      = true;
            settings.IndentChars = "   ";
            //settings.NewLineHandling=NewLineHandling.None;//an attempt to not remove \r in strings.  Failed.
            //using the constructor decreases performance and leads to memory leaks.
            //But it makes the xml much more readable
            XmlWriter writer = XmlWriter.Create(strBuild, settings);
                        #else
            XmlWriter writer = XmlWriter.Create(strBuild);
                        #endif
            XmlSerializer serializer;
            if (classType == typeof(Color))
            {
                serializer = new XmlSerializer(typeof(int));
                serializer.Serialize(writer, ((Color)obj).ToArgb());
            }
            else if (classType == typeof(TimeSpan))
            {
                serializer = new XmlSerializer(typeof(long));
                serializer.Serialize(writer, ((TimeSpan)obj).Ticks);
            }
            else
            {
                if (obj != null)
                {
                    obj = XmlConverter.XmlEscapeRecursion(classType, obj);                 //search object for string fields to escape
                }
                serializer = new XmlSerializer(classType);
                serializer.Serialize(writer, obj);
            }
            writer.Close();
            return(strBuild.ToString());
            //the result will be fully qualified xml, including declaration.  Example:

            /*
             * {<?xml version="1.0" encoding="utf-16"?>
             * <Userod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
             * <IsNew>false</IsNew>
             * <UserNum>1</UserNum>
             * <UserName>Admin</UserName>
             * <Password />
             * <UserGroupNum>1</UserGroupNum>
             * <EmployeeNum>0</EmployeeNum>
             * <ClinicNum>0</ClinicNum>
             * <ProvNum>0</ProvNum>
             * <IsHidden>false</IsHidden>
             * <TaskListInBox>0</TaskListInBox>
             * <AnesthProvType>3</AnesthProvType>
             * <DefaultHidePopups>false</DefaultHidePopups>
             * <PasswordIsStrong>false</PasswordIsStrong>
             * </Userod>}*/
        }
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            XmlSerializer keySerializer   = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

            foreach (TKey key in this.Keys)
            {
                writer.WriteStartElement("I");                //Item
                writer.WriteStartElement("K");                //Key
                keySerializer.Serialize(writer, (TKey)XmlConverter.XmlEscapeRecursion(typeof(TKey), key));
                writer.WriteEndElement();
                writer.WriteStartElement("V");                //Value
                valueSerializer.Serialize(writer, (TValue)XmlConverter.XmlEscapeRecursion(typeof(TValue), this[key]));
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
        }
Example #3
0
        public string Serialize()
        {
            if (this.Params != null)
            {
                foreach (DtoObject dtoCur in Params.Where(x => x != null && x.Obj != null))
                {
                    dtoCur.Obj = XmlConverter.XmlEscapeRecursion(dtoCur.Obj.GetType(), dtoCur.Obj);
                }
            }
            StringBuilder strBuild   = new StringBuilder();
            XmlWriter     writer     = XmlWriter.Create(strBuild);
            XmlSerializer serializer = new XmlSerializer(this.GetType());

            serializer.Serialize(writer, this);
            writer.Close();
            return(strBuild.ToString());
        }