接口应用中,传入传出数据以xml 字符串形式进行传输
将对象序列化转换为字符串传输方式:
XmlUtil.Serializer(typeof(T), t); //(T为类型 t为此类型对象,也就是要传输的数据对象) XmlUtil帮助类源码在下面
然后 将接到的xml字符串转换为对象函数:
public static T GetObj<T>(string respxml) where T : class,new()
{
byte[] array = Encoding.UTF8.GetBytes(respxml);
MemoryStream stream = new MemoryStream(array); //convert stream 2 string
StreamReader reader = new StreamReader(stream);
XmlSerializer xmlSearializer = new XmlSerializer(typeof(T));
T info = (T)xmlSearializer.Deserialize(reader);
return info;
}
其中如果要传输Base64Stream 二进制流字符串 在接收转换二进制时 会 抛出“base-64 字符数组的无效长度”这样的错误
对比了一下经过Convert.ToBase64String()转换过的参数发现,+都变成了空格
于是在传递参数前,使用Replace("+", "%2B"),目标页使用Replace("%2B", "+"),这样一替换,问题就解决
------------------------
XmlUtil xml序列化 帮助类
/// <summary>
/// Xml序列化与反序列化
/// </summary>
public class XmlUtil
{
#region 反序列化
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="xml">XML字符串</param>
/// <returns></returns>
public static object Deserialize(Type type, string xml)
{
try
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(sr);
}
}
catch (Exception e)
{
return null;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static object Deserialize(Type type, Stream stream)
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(stream);
}
#endregion
#region 序列化
/// <summary>
/// 序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer(Type type, object obj)
{
MemoryStream Stream = new MemoryStream();
XmlSerializer xml = new XmlSerializer(type);
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 XmlDocument GetXmlDocument(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return doc;
}
/// <summary>
///
/// </summary>
/// <param name="xml"></param>
/// <param name="selNodeName"></param>
/// <returns></returns>
public static string GetByName(string xml, string selNodeName)
{
string value = string.Empty;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode xmlNode = doc.SelectSingleNode(selNodeName);
if (xmlNode == null)
return string.Empty;
return xmlNode.InnerText;
}
/// <summary>
/// 获取单独的值
/// </summary>
/// <param name="xml"></param>
/// <param name="selNodeName"></param>
/// <param name="attributename"></param>
/// <returns></returns>
public static string GetAttributename(string xml, string selNodeName, string attributename)
{
string value = string.Empty;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList nodelist = doc.SelectNodes(selNodeName);
foreach (XmlNode node in nodelist)
{
XmlElement xl = node as XmlElement;
XmlNode firstChild = xl.FirstChild;
value = firstChild.InnerText;
}
return value;
}
/// <summary>
/// 获取多个值
/// </summary>
/// <param name="xml"></param>
/// <param name="selNodeName"></param>
/// <param name="attributename"></param>
/// <returns></returns>
public static List<string> GetAttribute(string xml, string selNodeName, List<string> attributename)
{
List<string> retVal = new List<string>();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList nodelist = doc.SelectSingleNode(selNodeName).ChildNodes;
foreach (XmlNode node in nodelist)
{
XmlElement xl = node as XmlElement;
foreach (string item in attributename)
{
string value = xl.GetAttribute(item);
retVal.Add(value);
}
}
return retVal;
}
public static StreamReader GetReader(string xmlDoc)
{
byte[] array = Encoding.UTF8.GetBytes(xmlDoc);
MemoryStream stream = new MemoryStream(array);
StreamReader reader = new StreamReader(stream);
return reader;
}
}