程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c# XML和實體類之間相互轉換

c# XML和實體類之間相互轉換

編輯:C#入門知識

 

今天寫了一個工具,可以在把實體類轉換為XML,同時也可以把XML轉換為對應的實體類,希望對大家有幫助

 

代碼如下

 

XML轉換類

 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.Data; 

using System.IO; 

using System.Xml.Serialization; 

using System.Xml; 

 

namespace WFXML 

    public class XmlUtil 

    { 

     

        #region 反序列化 

        /// <summary> 

        /// 反序列化 

        /// </summary> 

        /// <param name="type">類型</param> 

        /// <param name="xml">XML字符串</param> 

        /// <returns></returns> 

        public static object Deserialize(Type type, string xml) 

        { 

            try 

            { 

                using (StringReader sr = new StringReader(xml)) 

                { 

                    XmlSerializer xmldes = new XmlSerializer(type); 

                    return xmldes.Deserialize(sr); 

                } 

            } 

            catch (Exception e) 

            { 

 

                return null; 

            } 

        } 

        /// <summary> 

        /// 反序列化 

        /// </summary> 

        /// <param name="type"></param> 

        /// <param name="xml"></param> 

        /// <returns></returns> 

        public static object Deserialize(Type type, Stream stream) 

        { 

            XmlSerializer xmldes = new XmlSerializer(type); 

            return xmldes.Deserialize(stream); 

        } 

        #endregion 

 

        #region 序列化XML文件 

        /// <summary> 

        /// 序列化XML文件 

        /// </summary> 

        /// <param name="type">類型</param> 

        /// <param name="obj">對象</param> 

        /// <returns></returns> 

        public static string Serializer(Type type, object obj) 

        { 

            MemoryStream Stream = new MemoryStream(); 

            //創建序列化對象 

            XmlSerializer xml = new XmlSerializer(type); 

            try 

            { 

                //序列化對象 

                xml.Serialize(Stream, obj); 

            } 

            catch (InvalidOperationException) 

            { 

                throw; 

            } 

            Stream.Position = 0; 

            StreamReader sr = new StreamReader(Stream); 

            string str = sr.ReadToEnd(); 

            return str; 

        } 

        #endregion 

 

        #region 將XML轉換為DATATABLE 

        /// <summary> 

        /// 將XML轉換為DATATABLE 

        /// </summary> 

        /// <param name="FileURL"></param> 

        /// <returns></returns> 

        public static DataTable XmlAnalysisArray() 

        { 

            try 

            { 

                string FileURL = System.Configuration.ConfigurationManager.AppSettings["Client"].ToString(); 

                DataSet ds = new DataSet(); 

                ds.ReadXml(FileURL); 

                return ds.Tables[0]; 

            } 

            catch (Exception ex) 

            { 

                System.Web.HttpContext.Current.Response.Write(ex.Message.ToString()); 

                return null; 

            } 

        } 

        /// <summary> 

        /// 將XML轉換為DATATABLE 

        /// </summary> 

        /// <param name="FileURL"></param> 

        /// <returns></returns> 

        public static DataTable XmlAnalysisArray(string FileURL) 

        { 

            try 

            { 

                DataSet ds = new DataSet(); 

                ds.ReadXml(FileURL); 

                return ds.Tables[0]; 

            } 

            catch (Exception ex) 

            { 

                System.Web.HttpContext.Current.Response.Write(ex.Message.ToString()); 

                return null; 

            } 

        } 

        #endregion 

 

        #region 獲取對應XML節點的值 

        /// <summary> 

        /// 摘要:獲取對應XML節點的值 

        /// </summary> 

        /// <param name="stringRoot">XML節點的標記</param> 

        /// <returns>返回獲取對應XML節點的值</returns> 

        public static string XmlAnalysis(string stringRoot, string xml) 

        { 

            if (stringRoot.Equals("") == false) 

            { 

                try 

                { 

                    XmlDocument XmlLoad = new XmlDocument(); 

                    XmlLoad.LoadXml(xml); 

                    return XmlLoad.DocumentElement.SelectSingleNode(stringRoot).InnerXml.Trim(); 

                } 

                catch (Exception ex) 

                { 

                    

                } 

            } 

            return ""; 

        } 

        #endregion 

 

 

    } 

 

 

 

 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Linq; 

using System.Text; 

using System.Windows.Forms; 

 

namespace WFXML 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        } 

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            //把XML文件轉換為對應的實體類 

            string xml = @"<Depart> 

                          <DepartID>123</DepartID> 

                         <PerSons> 

                         <PerSon> 

                         <name>張三</name> 

                         <age>3</age> 

                      </PerSon> 

                         <PerSon> 

                         <name>李斯</name> 

                         <age>56</age> 

                         </PerSon> 

                         </PerSons> 

                         </Depart>  

"; 

 

            var Info = (Depart)XmlUtil.Deserialize(typeof(Depart), xml); 

 

            MessageBox.Show(Info.DepartID); 

        } 

 

        private void button2_Click(object sender, EventArgs e) 

        { 

            //把實體類轉換為XML 

            Depart dp = new Depart(); 

            dp.DepartID = "qq"; 

            dp.DepartID = "123"; 

 

            PerSon p1 = new PerSon(); 

            p1.name = "zhang"; 

            p1.age = "4"; 

            dp.PerSons[0] = p1; 

      

 

         string resutl=XmlUtil.Serializer(typeof(Depart), dp); 

         MessageBox.Show(resutl); 

        } 

    } 

 

    public class Depart 

    { 

        public string DepartID; 

        public PerSon[] PerSons=new PerSon[2]; 

    } 

 

    public class PerSon 

    { 

       public string name; 

 

        public string age; 

       

    } 

  

摘自 奶酪專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved