C # XML Serialization helper Class code

Source: Internet
Author: User

 Public Static classXmlhelper {Private Static voidXmlserializeinternal (Stream stream,Objecto, Encoding Encoding) {            if(O = =NULL)                Throw NewArgumentNullException ("o"); if(Encoding = =NULL)                Throw NewArgumentNullException ("encoding"); XmlSerializer Serializer=NewXmlSerializer (O.gettype ()); XmlWriterSettings Settings=Newxmlwritersettings (); Settings. Indent=true; Settings. Newlinechars="\ r \ n"; Settings. Encoding=encoding; Settings. IndentChars="    "; using(XmlWriter writer =XmlWriter.Create (stream, settings)) {Serializer.                Serialize (writer, o); Writer.            Close (); }        }        /// <summary>        ///serializes an object into an XML string/// </summary>        /// <param name= "O" >the object to serialize</param>        /// <param name= "encoding" >Encoding Method</param>        /// <returns>serialization of the resulting XML string</returns>         Public Static stringXmlserialize (Objecto, Encoding Encoding) {            using(MemoryStream stream =NewMemoryStream ())                {xmlserializeinternal (stream, O, encoding); Stream. Position=0; using(StreamReader reader =NewStreamReader (stream, encoding)) {                    returnReader.                ReadToEnd (); }            }        }        /// <summary>        ///writes an object to a file in XML serialized format/// </summary>        /// <param name= "O" >the object to serialize</param>        /// <param name= "path" >Save File path</param>        /// <param name= "encoding" >Encoding Method</param>         Public Static voidXmlserializetofile (ObjectOstringpath, Encoding Encoding) {            if(string. IsNullOrEmpty (path))Throw NewArgumentNullException ("Path"); using(FileStream file =NewFileStream (Path, FileMode.Create, FileAccess.Write))            {xmlserializeinternal (file, O, encoding); }        }        /// <summary>        ///deserializing an object from an XML string/// </summary>        /// <typeparam name= "T" >Result Object Type</typeparam>        /// <param name= "S" >the XML string that contains the object</param>        /// <param name= "encoding" >Encoding Method</param>        /// <returns>deserialization of the resulting object</returns>         Public StaticT xmldeserialize<t> (strings, Encoding Encoding) {            if(string. IsNullOrEmpty (s))Throw NewArgumentNullException ("s"); if(Encoding = =NULL)                Throw NewArgumentNullException ("encoding"); XmlSerializer Myserializer=NewXmlSerializer (typeof(T)); using(MemoryStream ms =NewMemoryStream (encoding. GetBytes (s))) {using(StreamReader sr =NewStreamReader (MS, encoding)) {                    return(T) myserializer.deserialize (SR); }            }        }        /// <summary>        ///reads a file and deserializes the object in the same way as XML. /// </summary>        /// <typeparam name= "T" >Result Object Type</typeparam>        /// <param name= "path" >file path</param>        /// <param name= "encoding" >Encoding Method</param>        /// <returns>deserialization of the resulting object</returns>         Public StaticT xmldeserializefromfile<t> (stringpath, Encoding Encoding) {            if(string. IsNullOrEmpty (path))Throw NewArgumentNullException ("Path"); if(Encoding = =NULL)                Throw NewArgumentNullException ("encoding"); stringXML =file.readalltext (path, encoding); returnXmldeserialize<t>(XML, encoding); }    }

C # XML Serialization helper Class code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.