Serializing XML as an object

Source: Internet
Author: User

1

Namespace Mvcapptest.controllers {public class Homecontroller:controller {////Get:/home/ Public ActionResult Index () {String sql = ' Select A.*, (select Username,password, Error from

            T_userlogin where Loginid=a.loginid FOR XML auto, type, elements) as Userinfoxml from T_userinfo a ";

            list<t_userinfo> list = sqlhelper.executeclass<t_userinfo> (sql, NULL); The value of the Userinfoxml field after the SQL statement query is://<T_UserLogin><UserName> salt Free Sea </username><password>12345 6</password><error>1</error></t_userlogin>//Converting XML-formatted strings to objects (pay special attention to <T_UserLogin> To be consistent with the target class name) var objlist = homecontroller.xmltoobject<t_userlogin> (list. A ().
            Userinfoxml, Encoding.UTF8);
            var name = Objlist.username;
        return View ();
        ///<summary>///Serializes object objects into XML///</summary><typeparam name= "T" > objects that need to be serialized </typeparam>///<param name= "Encoding" > If serialization fails, you can try to modify the lower encoding Value try </param>///<returns></returns> public static string objecttoxml<t> (T T, encod
            ing encoding) {XmlSerializer ser = new XmlSerializer (T.gettype ()); using (MemoryStream mem = new MemoryStream ()) {using (XmlTextWriter writer = new Xmltextwrite
                    R (Mem, encoding)) {xmlserializernamespaces ns = new XmlSerializerNamespaces (); Ns.
                    Add ("", ""); Ser.
                    Serialize (writer, T, NS); return encoding. GetString (Mem. ToArray ()).
                Trim (); ///<summary>///XML deserialization to object///</summary>///&L T;typeparam name= "T" ></typeparam>///<param name= "source" > XML string to be serialized </param>///& Ltparam name= "Encoding" > If serialization error, you can try to modify the value of the next encoding to try </param>///<returns></returns> Pub  Lic static T xmltoobject<t> (string source, Encoding Encoding) {XmlSerializer myserializer = new
            XmlSerializer (typeof (T)); using (MemoryStream stream = new MemoryStream (encoding).
            GetBytes (source)) {return (T) myserializer.deserialize (stream); }
        }
    }
}

Namespace Mvcapptest.dal
{
    [Serializable] public
    class T_userinfo
    {public
        int Id {get; set;}

        public string UserName {get; set;}

        public int Age {get; set;}

        public string Userinfoxml {get; set;}
    }
    [Serializable]
    public class T_userlogin
    {public
        string UserName {get; set;}

        public string Password {get; set;}

        public int Error {get; set;}}}


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.