XmlExtention抽离到单独文件中

This commit is contained in:
Kevin
2017-02-19 20:05:35 +08:00
parent cb68f973f4
commit 4500ba2b18
3 changed files with 88 additions and 81 deletions
-81
View File
@@ -13,10 +13,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace OSS.Common.Extention
{
@@ -564,82 +561,4 @@ namespace OSS.Common.Extention
}
}
/// <summary>
/// xml 转化扩展
/// </summary>
public static class XmlConvertExtention
{
/// <summary>
/// 对象序列化成xml字符串
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string SerializeToXml(this object t)
{
string result = string.Empty;
MemoryStream stream = null;
StreamReader streamReader = null;
try
{
var serializer = new XmlSerializer(t.GetType());
stream = new MemoryStream();
serializer.Serialize(stream, t);
stream.Position = 0;
streamReader = new StreamReader(stream);
result = streamReader.ReadToEnd();
}
finally
{
streamReader?.Dispose();
stream?.Dispose();
}
return result;
}
/// <summary>
/// xml字符串转化到对象
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="xml"></param>
/// <returns></returns>
public static TT DeserializeXml<TT>(this string xml)
where TT:class
{
return DeserializeXml(xml, typeof(TT)) as TT;
}
/// <summary>
/// xml字符串转化到对象
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static object DeserializeXml(this string xml,Type type)
{
object result = null;
MemoryStream stream = null;
StreamWriter writer = null;
try
{
stream = new MemoryStream();
writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
var deSer = new XmlSerializer(type);
result = deSer.Deserialize(stream);
}
finally
{
writer?.Dispose();
stream?.Dispose();
}
return result;
}
}
}
+87
View File
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace OSS.Common.Extention
{
/// <summary>
/// xml 转化扩展
/// </summary>
public static class XmlConvertExtention
{
/// <summary>
/// 对象序列化成xml字符串
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string SerializeToXml(this object t)
{
string result = string.Empty;
MemoryStream stream = null;
StreamReader streamReader = null;
try
{
var serializer = new XmlSerializer(t.GetType());
stream = new MemoryStream();
serializer.Serialize(stream, t);
stream.Position = 0;
streamReader = new StreamReader(stream);
result = streamReader.ReadToEnd();
}
finally
{
streamReader?.Dispose();
stream?.Dispose();
}
return result;
}
/// <summary>
/// xml字符串转化到对象
/// </summary>
/// <typeparam name="TT"></typeparam>
/// <param name="xml"></param>
/// <returns></returns>
public static TT DeserializeXml<TT>(this string xml)
where TT : class
{
return DeserializeXml(xml, typeof(TT)) as TT;
}
/// <summary>
/// xml字符串转化到对象
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static object DeserializeXml(this string xml, Type type)
{
object result = null;
MemoryStream stream = null;
StreamWriter writer = null;
try
{
stream = new MemoryStream();
writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
var deSer = new XmlSerializer(type);
result = deSer.Deserialize(stream);
}
finally
{
writer?.Dispose();
stream?.Dispose();
}
return result;
}
}
}
+1
View File
@@ -58,6 +58,7 @@
<Compile Include="Extention\UrlExtension.cs" />
<Compile Include="Extention\Volidate\VolidateAttributes.cs" />
<Compile Include="Extention\Volidate\VolidateExtention.cs" />
<Compile Include="Extention\XmlExtention.cs" />
<Compile Include="Modules\CacheModule\Cache.cs" />
<Compile Include="Modules\CacheModule\CacheUtil.cs" />
<Compile Include="Modules\CacheModule\ICache.cs" />