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;}}}