C#_XML与Object转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Utility
{
public class XMLHelper
{
#region object --> XML string.
public static string ToRequestXML<T>(T obj) where T : class
{
string xmlStr = string.Empty;
try
{
var type = typeof(T);
var properties = type.GetProperties();
XmlDocument xd = new XmlDocument();
XmlElement xe = xd.CreateElement("msgBody");
for (int i = 0; i < properties.Length; i++)
{
var elementName = properties[i].Name;
#region current type is List
if (properties[i].PropertyType.FullName.Contains("System.Collections.Generic.List"))
{
var propertyValue = properties[i].GetValue(obj, null);
if (propertyValue == null)
{
XmlElement xa = xd.CreateElement(elementName);
xe.AppendChild(xa);
}
else
{
xe = getArray(propertyValue, xe, xd, true, true, elementName);
}
continue;
}
#endregion
#region current type is Array
if (properties[i].PropertyType.BaseType.Name == "Array")
{
var propertyName = properties[i].Name;
var propertyValue = properties[i].GetValue(obj, null);
if (propertyValue == null)
{
XmlElement xa = xd.CreateElement(elementName);
xe.AppendChild(xa);
}
else
{
xe = getArray(propertyValue, xe, xd, true, false, elementName);
}
continue;
}
#endregion
#region current type is Model
if (properties[i].PropertyType.BaseType.Name != "Array" && (properties[i].PropertyType != typeof(long) && properties[i].PropertyType != typeof(int) && properties[i].PropertyType != typeof(string)) && !properties[i].PropertyType.FullName.Contains("System.Collections.Generic.List"))
{
var childPropertyName = properties[i].Name;
var childPropertyValue = properties[i].GetValue(obj, null);
if (childPropertyValue == null)
{
var modelType = properties[i].PropertyType.UnderlyingSystemType;
var model = modelType.Assembly.CreateInstance(modelType.FullName);
xe = getArray(model, xe, xd, false, false, elementName);
}
else
{
xe = getArray(childPropertyValue, xe, xd, false, false, elementName);
}
continue;
}
#endregion
#region current type is int or string
if (properties[i].PropertyType == typeof(long) || properties[i].PropertyType == typeof(string) || properties[i].PropertyType == typeof(int)) {
var childPropertyName = properties[i].Name;
var childPropertyValue = properties[i].GetValue(obj, null);
XmlElement xa = xd.CreateElement(childPropertyName);
if (childPropertyValue != null)
{
xa.InnerText = childPropertyValue.ToString();
}
else
{
xa.InnerText = "";
}
xe.AppendChild(xa);
continue;
}
#endregion
}
xd.AppendChild(xe);
xmlStr = xd.InnerXml;
return xmlStr;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region Get array value
private static XmlElement getArray(object propertyValue, XmlElement xe, XmlDocument xd, bool isArray, bool isList , string elementName)
{
try
{
if (propertyValue != null)
{
if (isList)
{
#region current type is Array
var array = propertyValue as System.Collections.IEnumerable;
if (array != null)
{
foreach (var item in array)
{
var obj = item;
var type = item.GetType();
var properties = type.GetProperties();
XmlElement childXe = xd.CreateElement(elementName);
for (int i = 0; i < properties.Length; i++)
{
var propertyType = properties[i].PropertyType;
#region current type is List
if (properties[i].PropertyType.FullName.Contains("System.Collections.Generic.List"))
{
var elementName2 = properties[i].Name;
var propertyValue2 = properties[i].GetValue(obj, null);
if (propertyValue2 == null)
{
XmlElement xa = xd.CreateElement(elementName);
childXe.AppendChild(xa);
}
else
{
childXe = getArray(propertyValue2, childXe, xd, true, true, elementName2);
}
continue;
}
#endregion
#region current type is Array
if (properties[i].PropertyType.BaseType.Name == "Array")
{
var elementName2 = properties[i].Name;
var childPropertyValue = properties[i].GetValue(obj, null);
if (childPropertyValue == null)
{
XmlElement xa = xd.CreateElement(elementName);
childXe.AppendChild(xa);
}
else
{
childXe = getArray(childPropertyValue, childXe, xd, true, false, elementName2);
}
}
#endregion
#region current type is Model
if (properties[i].PropertyType.BaseType.Name != "Array" && (properties[i].PropertyType != typeof(long) && properties[i].PropertyType != typeof(int) && properties[i].PropertyType != typeof(string)) &&
!properties[i].PropertyType.FullName.Contains("System.Collections.Generic.List"))
{
elementName = properties[i].Name;
var childPropertyValue = properties[i].GetValue(obj, null);
if (childPropertyValue == null)
{
var modelType = type.UnderlyingSystemType;
var model = modelType.Assembly.CreateInstance(modelType.FullName);
xe = getArray(model, xe, xd, false, false, elementName);
}
else
{
childXe = getArray(childPropertyValue, childXe, xd, false, false, elementName);
}
}
#endregion
#region current type is Number
if (properties[i].PropertyType == typeof(long) || properties[i].PropertyType == typeof(string) || properties[i].PropertyType == typeof(int))
{
var childPropertyName = properties[i].Name;
var childPropertyValue = properties[i].GetValue(obj, null);
XmlElement xa = xd.CreateElement(childPropertyName);
if (childPropertyValue != null)
{
xa.InnerText = childPropertyValue.ToString();
}
else
{
xa.InnerText = "";
}
childXe.AppendChild(xa);
}
#endregion
}
xe.AppendChild(childXe);
}
}
else
{
var list = (List<object>)propertyValue;
}
#endregion
return xe;
}
if (isArray)
{
typeof array#region current type is Array
var array = (Array)propertyValue;
if (array != null)
{
for (int i = 0; i < array.Length; i++)
{
var obj = array.GetValue(i);
var type = array.GetValue(i).GetType();
var properties = type.GetProperties();
XmlElement childXe = xd.CreateElement(elementName);
for (int j = 0; j < properties.Length; j++)
{
var propertyType = properties[j].PropertyType;
#region current type is List
if (properties[j].PropertyType.FullName.Contains("System.Collections.Generic.List"))
{
var elementName2 = properties[j].Name;
var propertyValue2 = properties[j].GetValue(obj, null);
if (propertyValue2 == null)
{
XmlElement xa = xd.CreateElement(elementName);
childXe.AppendChild(xa);
}
else
{
childXe = getArray(propertyValue2, childXe, xd, true, true, elementName2);
}
continue;
}
#endregion
#region current type is Array
if (properties[j].PropertyType.BaseType.Name == "Array")
if (properties[j].PropertyType.BaseType.Name == "Array")
{
var elementName2 = properties[j].Name;
var childPropertyValue = properties[j].GetValue(obj, null);
if (childPropertyValue == null)
{
XmlElement xa = xd.CreateElement(elementName);
childXe.AppendChild(xa);
}
else
{
childXe = getArray(childPropertyValue, childXe, xd, true, false, elementName2);
}
}
#endregion
#region current type is Model
if (properties[j].PropertyType.BaseType.Name != "Array" && (properties[j].PropertyType != typeof(long) &&
properties[j].PropertyType != typeof(int) && properties[j].PropertyType != typeof(string)))
{
elementName = properties[j].Name;
var childPropertyValue = properties[j].GetValue(obj, null);
if (childPropertyValue == null)
{
var modelType = properties[i].PropertyType.UnderlyingSystemType;
var model = modelType.Assembly.CreateInstance(modelType.FullName);
xe = getArray(model, xe, xd, false, false, elementName);
}
else
{
childXe = getArray(childPropertyValue, childXe, xd, false, false, elementName);
}
}
#endregion
#region current type is Number
if (properties[j].PropertyType == typeof(long) || properties[j].PropertyType == typeof(string) || properties[j].PropertyType == typeof(int))
{
var childPropertyName = properties[j].Name;
var childPropertyValue = properties[j].GetValue(obj, null);
XmlElement xa = xd.CreateElement(childPropertyName);
if (childPropertyValue != null)
{
xa.InnerText = childPropertyValue.ToString();
}
else
{
xa.InnerText = "";
}
childXe.AppendChild(xa);
}
#endregion
}
xe.AppendChild(childXe);
}
}
else
{
var list = (List<object>)propertyValue;
}
#endregion
}
else
{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论