public classXmlHelper
{///
///根据模型生成一个xml,其中不包括类名///
/// 模型
/// 属性值为空,xml生成是否返回该属性
///
public static StringBuilder ModelToXml(objectModel, Flag flag)
{
StringBuilder Xml= newStringBuilder();
Type t=Model.GetType();
System.Reflection.PropertyInfo[] properties=t.GetProperties();foreach (var item inproperties)
{var PropertiesValue = item.GetValue(Model, null);string PropertiesName =item.Name;if (PropertiesValue == null || string.IsNullOrEmpty(PropertiesValue.ToString()))
{if (flag==Flag.Y)
{
Xml.AppendFormat("{1}{0}>", PropertiesName, PropertiesValue);
}
}else{
Xml.AppendFormat("{1}{0}>", PropertiesName, PropertiesValue);
}
}returnXml;
}///
///根据模型生成一个xml,其他不包括类名///
/// 模型
/// 属性值为空,xml生成是否返回该属性
/// 对属性名称排序/可选(升序、降序)
///
public static StringBuilder ModelToXml(object Model, Flag flag,Sort sort=Sort.Asc)
{
StringBuilder Xml= newStringBuilder();
Type t=Model.GetType();
System.Reflection.PropertyInfo[] properties=t.GetProperties();
Dictionary dic = new Dictionary();foreach (var item inproperties)
{var PropertiesValue = item.GetValue(Model, null);string PropertiesName =item.Name;
dic.Add(PropertiesName, PropertiesValue);
}//对属性排序
var SortList = sort == Sort.Asc ? dic.OrderBy(o => o.Key) : dic.OrderByDescending(o =>o.Key);foreach (var item inSortList)
{if (item.Value == null || string.IsNullOrEmpty(item.Value.ToString()))
{if (flag ==Flag.Y)
{
Xml.AppendFormat("{1}{0}>", item.Key, item.Value);
}
}else{
Xml.AppendFormat("{1}{0}>", item.Key, item.Value);
}
}returnXml;
}///
///根据模型生成一个xml,其他不包括类名///
/// 模型
/// 属性值为空,xml生成是否返回该属性
/// 对属性名称排序/可选(升序、降序)
/// 父节点名称
///
public static StringBuilder ModelToXml(object Model, Flag flag, Sort sort,stringRootName)
{
StringBuilder Xml= newStringBuilder();
Type t=Model.GetType();
System.Reflection.PropertyInfo[] properties=t.GetProperties();
Dictionary dic = new Dictionary();
Xml.AppendFormat("",RootName);foreach (var item inproperties)
{var PropertiesValue = item.GetValue(Model, null);string PropertiesName =item.Name;
dic.Add(PropertiesName, PropertiesValue);
}//对属性排序
var SortList = sort == Sort.Asc ? dic.OrderBy(o => o.Key) : dic.OrderByDescending(o =>o.Key);foreach (var item inSortList)
{if (item.Value == null || string.IsNullOrEmpty(item.Value.ToString()))
{if (flag ==Flag.Y)
{
Xml.AppendFormat("{1}{0}>", item.Key, item.Value);
}
}else{
Xml.AppendFormat("{1}{0}>", item.Key, item.Value);
}
}
Xml.AppendFormat("{0}>", RootName);returnXml;
}
}///
///枚举///
public enumFlag
{///
///是///
Y,///
///否///
N
};///
///排序///
public enumSort
{///
///升序///
Asc,///
///降序///
Desc
};public classToo
{//编码内容
public static string Base64Encode(stringstr)
{byte[] b =System.Text.Encoding.Default.GetBytes(str);//转成Base64形式的System.String
returnConvert.ToBase64String(b);
}//解码内容
public static string Base64Decode(stringstr)
{byte[] outputb =Convert.FromBase64String(str);string orgStr =Encoding.Default.GetString(outputb);returnorgStr;
}
}