コード例 #1
0
        /// <summary>
        /// Serializes the <i>Obj</i> to an XML string.
        /// </summary>
        /// <param name="Obj">
        /// The object to serialize.</param>
        /// <param name="ObjType">The object type.</param>
        /// <returns>
        /// The serialized object XML string.
        /// </returns>
        /// <remarks>
        /// The <see cref="PrettyPrint" /> property provides
        /// an easy-to-read formatted XML string.
        /// </remarks>
        public static string ToXml(object Obj, System.Type ObjType)
        {
            XmlSerializer ser;

            ser = new XmlSerializer(ObjType, PACTSerializer.TargetNamespace);
            MemoryStream memStream;

            memStream = new MemoryStream();
            XmlTextWriter xmlWriter;

            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            if (PACTSerializer.PrettyPrint)
            {
                xmlWriter.Formatting  = Formatting.Indented;
                xmlWriter.Indentation = 1;
                xmlWriter.IndentChar  = Convert.ToChar(9);
            }
            xmlWriter.Namespaces = true;
            ser.Serialize(xmlWriter, Obj, PACTSerializer.GetNamespaces());
            xmlWriter.Close();
            memStream.Close();
            string xml;

            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return(xml);
        }
コード例 #2
0
        /// <summary>
        /// Serializes the <i>Obj</i> to an XML string.
        /// </summary>
        /// <param name="Obj">
        /// The object to serialize.</param>
        /// <param name="ObjType">The object type.</param>
        /// <returns>
        /// The serialized object XML string.
        /// </returns>
        /// <remarks>
        /// The <see cref="PrettyPrint" /> property provides
        /// an easy-to-read formatted XML string.
        /// </remarks>
        public static string ToXml(object Obj, System.Type ObjType, bool AllowNameSpace)
        {
            XmlSerializer ser;

            if (AllowNameSpace)
            {
                ser = new XmlSerializer(ObjType, PACTSerializer.TargetNamespace);
            }
            else
            {
                ser = new XmlSerializer(ObjType, "");
            }
            MemoryStream memStream;

            memStream = new MemoryStream();

            XmlWriterSettings xmlws = new XmlWriterSettings();

            xmlws.OmitXmlDeclaration = true;
            xmlws.Encoding           = Encoding.UTF8;
            XmlWriter xmlWriter = XmlWriter.Create(memStream, xmlws);

            //xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            //xmlWriter.Settings.OmitXmlDeclaration = true;

            //if (PACTSerializer.PrettyPrint)
            //{
            //    xmlWriter.Formatting = Formatting.Indented;
            //    xmlWriter.Indentation = 1;
            //    xmlWriter.IndentChar = Convert.ToChar(9);

            //}
            //   xmlWriter.Namespaces = true;


            if (AllowNameSpace)
            {
                ser.Serialize(xmlWriter, Obj, PACTSerializer.GetNamespaces());
            }
            else
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                ser.Serialize(xmlWriter, Obj, ns);
            }

            xmlWriter.Close();
            memStream.Close();
            string xml;

            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return(xml);
        }