码迷,mamicode.com
首页 > 其他好文 > 详细

xml

时间:2016-07-02 13:18:08      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Data;
  7 using System.Xml;
  8 using System.Xml.Serialization;
  9 
 10 /// <summary>
 11 /// Xml序列化与反序列化
 12 /// </summary>
 13 public class XmlUtil
 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 
 50     public static object DeserializeXml(Type type, XmlDocument xml)
 51     {
 52 
 53         TextReader reader = new StringReader(xml.OuterXml);
 54         XmlSerializer xmldes = new XmlSerializer(type);
 55         return xmldes.Deserialize(reader);
 56     }
 57 
 58 
 59 
 60     #endregion
 61 
 62     #region 序列化
 63     /// <summary>
 64     /// 序列化
 65     /// </summary>
 66     /// <param name="type">类型</param>
 67     /// <param name="obj">对象</param>
 68     /// <returns></returns>
 69     public static string Serializer(Type type, object obj)
 70     {
 71         MemoryStream Stream = new MemoryStream();
 72         XmlSerializer xml = new XmlSerializer(type);
 73         try
 74         {
 75             //序列化对象
 76             xml.Serialize(Stream, obj);
 77         }
 78         catch (InvalidOperationException)
 79         {
 80             throw;
 81         }
 82         Stream.Position = 0;
 83         StreamReader sr = new StreamReader(Stream);
 84         string str = sr.ReadToEnd();
 85 
 86         sr.Dispose();
 87         Stream.Dispose();
 88 
 89         return str;
 90     }
 91     public static XmlDocument SerializerXml(Type type, object obj)
 92     {
 93         XmlDocument x= new XmlDocument();
 94      
 95         MemoryStream Stream = new MemoryStream();
 96         XmlSerializer xml = new XmlSerializer(type);
 97         try
 98         {
 99             //序列化对象
100             xml.Serialize(Stream, obj);
101         }
102         catch (InvalidOperationException)
103         {
104             throw;
105         }
106         
107         Stream.Position = 0;
108         StreamReader sr = new StreamReader(Stream);
109         string str = sr.ReadToEnd();
110 
111         x.LoadXml(str);
112         sr.Dispose();
113         Stream.Dispose();
114 
115         return x;
116     }
117 
118     #endregion
119 }

 

xml

标签:

原文地址:http://www.cnblogs.com/zhutiehan/p/5635128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!