XML processing aid class, encoding format GBK !!

Many interface transfer XML format conversion needs, then how to achieve? I will help to paste the following categories:

  ///  <Summary> 
    /// XML processing helper class !! encoding format GBK
     ///  </ Summary> 
    public  class XmlUtility 
    { 
        ///  <Summary> 
        /// custom XML string into a target sequence
         / //  </ Summary> 
        ///  <param name = "the myObject"> custom object entity </ param> 
        ///  <Returns> serialized XML string </ Returns> 
        public  static  string SerializeToXml <T> ( obj T) 
        { 
            the try 
            { 
                the MemoryStream MS = new new the MemoryStream ();
                StreamWriter sw = new StreamWriter(ms, Encoding.GetEncoding("GBK"));
                var ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                Type t = obj.GetType();
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(sw, obj, ns);

                string xmlMessage = Encoding.GetEncoding("GBK").GetString(ms.GetBuffer());
                sw.Close();
                ms.Close();
                return xmlMessage.ToString (); 
            } 
            the catch (Exception EX) 
            { 
                the throw  new new Exception ( " converts the object to an XML entity abnormal " , EX); 
            } 
        } 

        ///  <Summary> 
        /// the XML string deserialize Object
         ///  </ Summary> 
        ///  <typeParam name = "T"> Object type </ typeParam> 
        ///  <param name = "XML"> the XML character </ param> 
        ///  <Returns> </ Returns> 
        public  static T DeserializeToObject <T>(string xml) where T : class
        {
            try
            {
                using (StringReader sr = new StringReader(xml))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return serializer.Deserialize(sr) as T;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("将XML转换成实体对象异常", ex);
            }
        }
    }

According to the above class I simply wrote a Demo:

    public  class the HomeController: the Controller 
    { 
        public ActionResult Index () 
        { 
            // Get parameter 
            RequestModel = R & lt new new RequestModel (); 
            the UpdateModel <RequestModel> (R & lt);
             // splicing parameters to display text 
            String MSG r.name + = " year " + R & lt + .Age " years old " ;
             return the Content (MSG); 
        } 
    
        public ActionResult the About () 
        { 
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

 

Guess you like

Origin www.cnblogs.com/shuai7boy/p/10963734.html
Aid