分享

c# XML和实体类之间相互转换

 ThinkTank_引擎 2014-09-16

今天写了一个工具,可以在把实体类转换为XML,同时也可以把XML转换为对应的实体类,希望对大家有帮助

代码如下

 

XML转换类

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using System.IO;  
  7. using System.Xml.Serialization;  
  8. using System.Xml;  
  9.   
  10. namespace WFXML  
  11. {  
  12.     public class XmlUtil  
  13.     {  
  14.       
  15.         #region 反序列化  
  16.         /// <summary>  
  17.         /// 反序列化  
  18.         /// </summary>  
  19.         /// <param name="type">类型</param>  
  20.         /// <param name="xml">XML字符串</param>  
  21.         /// <returns></returns>  
  22.         public static object Deserialize(Type type, string xml)  
  23.         {  
  24.             try  
  25.             {  
  26.                 using (StringReader sr = new StringReader(xml))  
  27.                 {  
  28.                     XmlSerializer xmldes = new XmlSerializer(type);  
  29.                     return xmldes.Deserialize(sr);  
  30.                 }  
  31.             }  
  32.             catch (Exception e)  
  33.             {  
  34.   
  35.                 return null;  
  36.             }  
  37.         }  
  38.         /// <summary>  
  39.         /// 反序列化  
  40.         /// </summary>  
  41.         /// <param name="type"></param>  
  42.         /// <param name="xml"></param>  
  43.         /// <returns></returns>  
  44.         public static object Deserialize(Type type, Stream stream)  
  45.         {  
  46.             XmlSerializer xmldes = new XmlSerializer(type);  
  47.             return xmldes.Deserialize(stream);  
  48.         }  
  49.         #endregion  
  50.  
  51.         #region 序列化XML文件  
  52.         /// <summary>  
  53.         /// 序列化XML文件  
  54.         /// </summary>  
  55.         /// <param name="type">类型</param>  
  56.         /// <param name="obj">对象</param>  
  57.         /// <returns></returns>  
  58.         public static string Serializer(Type type, object obj)  
  59.         {  
  60.             MemoryStream Stream = new MemoryStream();  
  61.             //创建序列化对象  
  62.             XmlSerializer xml = new XmlSerializer(type);  
  63.             try  
  64.             {  
  65.                 //序列化对象  
  66.                 xml.Serialize(Stream, obj);  
  67.             }  
  68.             catch (InvalidOperationException)  
  69.             {  
  70.                 throw;  
  71.             }  
  72.             Stream.Position = 0;  
  73.             StreamReader sr = new StreamReader(Stream);  
  74.             string str = sr.ReadToEnd();  
  75.             return str;  
  76.         }  
  77.         #endregion  
  78.  
  79.         #region 将XML转换为DATATABLE  
  80.         /// <summary>  
  81.         /// 将XML转换为DATATABLE  
  82.         /// </summary>  
  83.         /// <param name="FileURL"></param>  
  84.         /// <returns></returns>  
  85.         public static DataTable XmlAnalysisArray()  
  86.         {  
  87.             try  
  88.             {  
  89.                 string FileURL = System.Configuration.ConfigurationManager.AppSettings["Client"].ToString();  
  90.                 DataSet ds = new DataSet();  
  91.                 ds.ReadXml(FileURL);  
  92.                 return ds.Tables[0];  
  93.             }  
  94.             catch (Exception ex)  
  95.             {  
  96.                 System.Web.HttpContext.Current.Response.Write(ex.Message.ToString());  
  97.                 return null;  
  98.             }  
  99.         }  
  100.         /// <summary>  
  101.         /// 将XML转换为DATATABLE  
  102.         /// </summary>  
  103.         /// <param name="FileURL"></param>  
  104.         /// <returns></returns>  
  105.         public static DataTable XmlAnalysisArray(string FileURL)  
  106.         {  
  107.             try  
  108.             {  
  109.                 DataSet ds = new DataSet();  
  110.                 ds.ReadXml(FileURL);  
  111.                 return ds.Tables[0];  
  112.             }  
  113.             catch (Exception ex)  
  114.             {  
  115.                 System.Web.HttpContext.Current.Response.Write(ex.Message.ToString());  
  116.                 return null;  
  117.             }  
  118.         }  
  119.         #endregion  
  120.  
  121.         #region 获取对应XML节点的值  
  122.         /// <summary>  
  123.         /// 摘要:获取对应XML节点的值  
  124.         /// </summary>  
  125.         /// <param name="stringRoot">XML节点的标记</param>  
  126.         /// <returns>返回获取对应XML节点的值</returns>  
  127.         public static string XmlAnalysis(string stringRoot, string xml)  
  128.         {  
  129.             if (stringRoot.Equals("") == false)  
  130.             {  
  131.                 try  
  132.                 {  
  133.                     XmlDocument XmlLoad = new XmlDocument();  
  134.                     XmlLoad.LoadXml(xml);  
  135.                     return XmlLoad.DocumentElement.SelectSingleNode(stringRoot).InnerXml.Trim();  
  136.                 }  
  137.                 catch (Exception ex)  
  138.                 {  
  139.                      
  140.                 }  
  141.             }  
  142.             return "";  
  143.         }  
  144.         #endregion  
  145.   
  146.   
  147.     }  
  148. }  


 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WFXML  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             //把XML文件转换为对应的实体类  
  22.             string xml = @"<Depart>  
  23.                           <DepartID>123</DepartID>  
  24.                          <PerSons>  
  25.                          <PerSon>  
  26.                          <name>张三</name>  
  27.                          <age>3</age>  
  28.                       </PerSon>  
  29.                          <PerSon>  
  30.                          <name>李斯</name>  
  31.                          <age>56</age>  
  32.                          </PerSon>  
  33.                          </PerSons>  
  34.                          </Depart>   
  35. ";  
  36.   
  37.             var Info = (Depart)XmlUtil.Deserialize(typeof(Depart), xml);  
  38.   
  39.             MessageBox.Show(Info.DepartID);  
  40.         }  
  41.   
  42.         private void button2_Click(object sender, EventArgs e)  
  43.         {  
  44.             //把实体类转换为XML  
  45.             Depart dp = new Depart();  
  46.             dp.DepartID = "qq";  
  47.             dp.DepartID = "123";  
  48.   
  49.             PerSon p1 = new PerSon();  
  50.             p1.name = "zhang";  
  51.             p1.age = "4";  
  52.             dp.PerSons[0] = p1;  
  53.        
  54.   
  55.          string resutl=XmlUtil.Serializer(typeof(Depart), dp);  
  56.          MessageBox.Show(resutl);  
  57.         }  
  58.     }  
  59.   
  60.     public class Depart  
  61.     {  
  62.         public string DepartID;  
  63.         public PerSon[] PerSons=new PerSon[2];  
  64.     }  
  65.   
  66.     public class PerSon  
  67.     {  
  68.        public string name;  
  69.   
  70.         public string age;  
  71.         
  72.     }  
  73.    
  74. }  


  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多