首先查看我们要序列化的Xml源文件:
按照上述节点模式定义数据结构:
[XmlRoot("RSS")] public class RSS { public Channel channel { get; set; } } [XmlRoot("channel")] public class Channel { public string title { get; set; } public string link { get; set; } public string description { get; set; } public string language { get; set; } public string lastBuildDate { get; set; } public string pubDate { get; set; } public string ttl { get; set; } public Channel_Item item { get; set; } } public class Channel_Item { public string title { get; set; } public string link { get; set; } [XmlElement("dc:creator")] public string dccreater { get; set; } public string author { get; set; } public string pubDate { get; set; } public string guid { get; set; } public string description { get; set; } }
序列化与反序列化用的方法:
/// <summary> /// 序列化 /// </summary> public static string Serialiaze(object obj) { StringBuilder xml = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(xml)) { XmlSerializer xs = new XmlSerializer(obj.GetType()); xs.Serialize(writer,obj); return xml.ToString(); } } /// <summary> /// 反序列化 /// </summary> public static T Deserialize<T>(string xmlContent) { XmlSerializer xs = new XmlSerializer(typeof(T)); using (StringReader strReader = new StringReader(xmlContent)) { XmlReader xmlReader = XmlReader.Create(strReader); return (T)xs.Deserialize(xmlReader); } }
使用上述方法,就能很容易的完成Xml与对象之间的转换,前提是,对象的数据结构定义要正确。
Xml与DataTable
上面说到Xml与对象之间的序列化与反序列,如果我们将参数传入的是DataTable,又会怎么样呢。我们先看看序列化DataTable的结果。
//DataTable 序列化与序列化 DataTable dt = new DataTable(); dt.TableName = "TestTab"; dt.Columns.Add("ID",typeof(int)); dt.Columns.Add("Value",typeof(string)); for (var i = 0; i < 10; i++) { var dr = dt.NewRow(); dr["ID"] = i + 1; dr["Value"] = DateTime.Now.Ticks.ToString(); dt.Rows.Add(dr); } //这样序列化DataTable得到的Xml不够简洁,关于DataTable与Xml互转 提供另外一种解决方案 xml = Serialiaze(dt);
此时对应的Xml的内容是:
这样看起来,是很不友好,但是功能上是没问题的,依旧可以正常的序列化与反序列化。可如果我们要将该DataTable序列化成可读性高的Xml呢,这也是有办法的。具体的方法则是DataTable与Xml互转,该方法是从互联网摘录的。
/// <summary> /// 将DataTable对象转换成XML字符串 /// </summary> /// <param name="dt">DataTable对象</param> /// <returns>XML字符串</returns> public static string DataToXml(DataTable dt) { if (dt != null) { MemoryStream ms = null; XmlTextWriter XmlWt = null; try { ms = new MemoryStream(); //根据ms实例化XmlWt XmlWt = new XmlTextWriter(ms,Encoding.Unicode); //获取ds中的数据 dt.WriteXml(XmlWt); int count = (int)ms.Length; byte[] temp = new byte[count]; ms.Seek(0,SeekOrigin.Begin); ms.Read(temp,0,count); //返回Unicode编码的文本 UnicodeEncoding ucode = new UnicodeEncoding(); return ucode.GetString(temp).Trim(); } catch (Exception ex) { throw ex; } finally { //释放资源 if (XmlWt != null) { ms.Close(); ms.Dispose(); } } } else { return string.Empty; } } /// <summary> /// 将Xml内容字符串转换成DataTable对象 /// </summary> /// <param name="xmlStr">Xml内容字符串</param> /// <returns>DataTable对象</returns> public static DataTable XmlToDataTable(string xmlStr) { if (!string.IsNullOrEmpty(xmlStr)) { StringReader StrStream = null; XmlTextReader Xmlrdr = null; try { DataSet ds = new DataSet(); //读取字符串中的信息 StrStream = new StringReader(xmlStr); //获取StrStream中的数据 Xmlrdr = new XmlTextReader(StrStream); //ds获取Xmlrdr中的数据 ds.ReadXml(Xmlrdr); return ds.Tables[0]; } catch (Exception ex) { throw ex; } finally { //释放资源 if (Xmlrdr != null) { Xmlrdr.Close(); StrStream.Close(); StrStream.Dispose(); } } } else { return null; } }
再来看看序列化后的结果吧:
这样看起来,是不是清晰了很多.
原文地址:http://www.cnblogs.com/codealone/archive/2013/04/17/3025454.html
原文链接:https://www.f2er.com/xml/300605.html