first commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace JSMachine.WMS.Infrastructure.Helper
|
||||
{
|
||||
public static class XmlHelper
|
||||
{
|
||||
public static T XmlToModel<T>(string xml) where T : class, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
xml = Regex.Replace(xml, @"<\?xml*.*?>", "", RegexOptions.IgnoreCase);
|
||||
XmlSerializer xmlSer = new(typeof(T));
|
||||
using (StringReader xmlReader = new(xml))
|
||||
{
|
||||
return (T)xmlSer.Deserialize(xmlReader);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
//throw new Exception("将XML字符串转换为实体异常", ex); ;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ModelToXml<T>(T obj) where T : class, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
MemoryStream stream = new();
|
||||
XmlSerializer xmlSer = new(typeof(T));
|
||||
xmlSer.Serialize(stream, obj);
|
||||
|
||||
stream.Position = 0;
|
||||
StreamReader sr = new(stream);
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
//throw new Exception("将实体对象转换成XML异常", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user