Exemplo n.º 1
0
		/// <summary>
		/// Generic Serialization method that will serialize object without the default namespaces:
		/// http://www.w3.org/2001/XMLSchema
		/// http://www.w3.org/2001/XMLSchema-instance
        /// This also ensures that the returned XML is always encoded in UTF-8.
		/// </summary>
		/// <param name="objDeserialized"></param>
		/// <param name="objectType"></param>
		/// <returns></returns>
		public static string Serialize(object objDeserialized, Type objectType)
		{
			//create empty namespaces so as to not render the default:
			//xmlns:xsd="http://www.w3.org/2001/XMLSchema"
			//xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            XmlSerializer xSer = new XmlSerializer(objectType);
            EncodedStringWriter sw = new EncodedStringWriter(new StringBuilder(), Encoding.UTF8);
            xSer.Serialize(sw, objDeserialized, ns);
            string str = sw.ToString();
            sw.Close();
            return str;
          
		}        
Exemplo n.º 2
0
        /// <summary>
        /// Generic Serialization method that will serialize object without the default namespaces:
        /// http://www.w3.org/2001/XMLSchema
        /// http://www.w3.org/2001/XMLSchema-instance
        /// This also ensures that the returned XML is always encoded in UTF-8.
        /// </summary>
        /// <param name="objDeserialized"></param>
        /// <param name="objectType"></param>
        /// <returns></returns>
        public static string Serialize(object objDeserialized, Type objectType)
        {
            //create empty namespaces so as to not render the default:
            //xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            //xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("", "");

            XmlSerializer       xSer = new XmlSerializer(objectType);
            EncodedStringWriter sw   = new EncodedStringWriter(new StringBuilder(), Encoding.UTF8);

            xSer.Serialize(sw, objDeserialized, ns);
            string str = sw.ToString();

            sw.Close();
            return(str);
        }