converting XML strings to objects in C # ASP

Source: Internet
Author: User

 Public classXmlutil {#regionDeserialization/// <summary>        ///deserializing an XML string into an object/// </summary>        /// <param name= "type" >type</param>        /// <param name= "xml" >XML string</param>        /// <returns></returns>         Public Static ObjectXml2obj (Type type,stringXML) {Try            {                using(StringReader sr =NewStringReader (XML)) {XmlSerializer Xmldes=NewXmlSerializer (type); returnXmldes.                Deserialize (SR); }            }            Catch(Exception e) {return NULL; }        }        #endregion        #regionObjects into/// <summary>        ///object into XML/// </summary>        /// <param name= "type" >type</param>        /// <param name= "obj" >Object</param>        /// <returns></returns>         Public Static stringObj2xml (Type type,Objectobj) {MemoryStream Stream=NewMemoryStream (); XmlSerializer XML=NewXmlSerializer (type); Try            {                //Serializing ObjectsXML.            Serialize (Stream, obj); }            Catch(InvalidOperationException) {Throw; } stream.position=0; StreamReader SR=NewStreamReader (Stream); stringstr =Sr.            ReadToEnd (); Sr.            Dispose ();            Stream.dispose (); returnstr; }        #endregion    }

Object class:

[XmlRoot ("people")]
public class Shuangchengpayqueryresponse
{
public string name {get; set;}
public string Age {get; set;}
} XML string: <xml><people><name>liming</name><age>25</age></people></xml >

converting XML strings to objects in C # ASP

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.