123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.IO;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.Serialization;
- namespace LYFZ.Weixin.SDK.Helpers
- {
- public static class XmlUtility
- {
- #region 反序列化
-
-
-
-
-
-
- public static object Deserialize<T>(string xml)
- {
- try
- {
- using (StringReader sr = new StringReader(xml))
- {
- XmlSerializer xmldes = new XmlSerializer(typeof(T));
- return xmldes.Deserialize(sr);
- }
- }
- catch
- {
- return null;
- }
- }
-
-
-
-
-
-
- public static object Deserialize<T>(Stream stream)
- {
- XmlSerializer xmldes = new XmlSerializer(typeof(T));
- return xmldes.Deserialize(stream);
- }
- #endregion
- #region 序列化
-
-
-
-
-
-
-
- public static string Serializer<T>(T obj)
- {
- MemoryStream Stream = new MemoryStream();
- XmlSerializer xml = new XmlSerializer(typeof(T));
- try
- {
-
- xml.Serialize(Stream, obj);
- }
- catch (InvalidOperationException)
- {
- throw;
- }
- Stream.Position = 0;
- StreamReader sr = new StreamReader(Stream);
- string str = sr.ReadToEnd();
- sr.Dispose();
- Stream.Dispose();
- return str;
- }
- #endregion
-
-
-
-
-
- public static XDocument Convert(Stream stream)
- {
- stream.Seek(0, SeekOrigin.Begin);
- using (XmlReader xr = XmlReader.Create(stream))
- {
- return XDocument.Load(xr);
- }
- }
- }
- }
|