C#递归生成XML文件,递归序列化对象
2016-06-14 20:44
330 查看
private static XmlElement CreateXmlTree(object obj, XmlDocument xDoc, XmlElement xElem)//
{
if (obj == null)
return null;
try
{
#region try...
var propertyInfos = obj.GetType().GetProperties();
XmlElement Node = xDoc.CreateElement(obj.GetType().Name);
foreach (PropertyInfo propertyInfo in propertyInfos)
{
var attributeArr = propertyInfo.GetCustomAttributes(true);
if (attributeArr != null)
{
#region case 1
if (ExistAttribute(attributeArr, "UIHXmlArrayAttributeAttribute") != null)
{
UIHXmlArrayAttributeAttribute attributeName = (UIHXmlArrayAttributeAttribute)ExistAttribute(attributeArr, "UIHXmlArrayAttributeAttribute");
string elementName = "";
if (attributeName.ElementName != null)
{
elementName = attributeName.ElementName;
}
else
{
elementName = propertyInfo.Name;
}
XmlElement xe = xDoc.CreateElement(elementName);
if (propertyInfo.PropertyType.IsGenericType)
{
object propertyValue = propertyInfo.GetValue(obj, null);
Type typeOfPropertyValue = propertyValue.GetType();
int numberOfListItem = Convert.ToInt32(typeOfPropertyValue.GetProperty("Count").GetValue(propertyValue, null));
for (int i = 0; i < numberOfListItem; i++)
{
object listItem = typeOfPropertyValue.GetProperty("Item").GetValue(propertyValue, new object[] { i });
xe = CreateXmlTree(listItem, xDoc, xe);
Node.AppendChild(xe);
}
}
else if (propertyInfo.PropertyType.IsClass)
{
object refObject = propertyInfo.GetValue(obj, null);
if (refObject != null)
{
Type objType = refObject.GetType();
xe = CreateXmlTree(refObject, xDoc, xe);
Node.AppendChild(xe);
}
}
}
#endregion
#region case 2
if (ExistAttribute(attributeArr, "UIHXmlAttributeAttribute") != null)
{
UIHXmlAttributeAttribute attributeName = (UIHXmlAttributeAttribute)ExistAttribute(attributeArr, "UIHXmlAttributeAttribute");
#region get element name
string elementName = "";
if (attributeName.AttributeName != null)
{
elementName = attributeName.AttributeName;
}
else
{
elementName = propertyInfo.Name;
}
#endregion
if (propertyInfo.GetValue(obj, null) != null)
{
#region get value of property
string propertyValue = "";
if (propertyInfo.PropertyType == typeof(TimeSpan))
{
DateTime start = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan timeSpan = (TimeSpan)propertyInfo.GetValue(obj, null);
DateTime ttt = start.Add(timeSpan);
propertyValue = ttt.ToString("yyyy/MM/dd-HH:mm:ss:fff");
}
else
{
propertyValue = propertyInfo.GetValue(obj, null).ToString();
}
#endregion
if (attributeName.DefaultValue == null)
{
XmlAttribute xe = xDoc.CreateAttribute(elementName);
xe.Value = propertyValue;
Node.Attributes.Append(xe);
}
else if (attributeName.DefaultValue != propertyValue)
{
XmlAttribute xe = xDoc.CreateAttribute(elementName);
xe.Value = propertyValue;
Node.Attributes.Append(xe);
}
}
}
#endregion
}
}
xElem.AppendChild(Node);
return xElem;
#endregion
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return null;
}
private static object ExistAttribute(object[] Attributes, string AttributeName)//找到特定的特性
{
for (int i = 0; i < Attributes.Length; ++i)
{
if (Attributes[i].GetType().Name == AttributeName)
{
return Attributes[i];
}
}
return null;
}
[Serializable]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Parameter |
AttributeTargets.Class | AttributeTargets.ReturnValue,AllowMultiple = false,Inherited = true)]
class UIHXmlAttributeAttribute:Attribute
{
public UIHXmlAttributeAttribute()
{
}
public UIHXmlAttributeAttribute(string elemName, string defaultValue)
{
AttributeName = elemName;
DefaultValue = defaultValue;
}
public string AttributeName { set; get; }
public string DefaultValue { set; get; }
}
[Serializable]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.ReturnValue | AttributeTargets.Class ,AllowMultiple = false)]
class UIHXmlArrayAttributeAttribute : Attribute
{
public UIHXmlArrayAttributeAttribute()
{
}
public UIHXmlArrayAttributeAttribute(string elemName,Type typeOfItem)
{
ElementName = elemName;
Type = typeOfItem;
}
public UIHXmlArrayAttributeAttribute(Type typeOfItem)
{
Type = typeOfItem;
}
public string ElementName {set;get;}
public Type Type { set; get; }
}
{
if (obj == null)
return null;
try
{
#region try...
var propertyInfos = obj.GetType().GetProperties();
XmlElement Node = xDoc.CreateElement(obj.GetType().Name);
foreach (PropertyInfo propertyInfo in propertyInfos)
{
var attributeArr = propertyInfo.GetCustomAttributes(true);
if (attributeArr != null)
{
#region case 1
if (ExistAttribute(attributeArr, "UIHXmlArrayAttributeAttribute") != null)
{
UIHXmlArrayAttributeAttribute attributeName = (UIHXmlArrayAttributeAttribute)ExistAttribute(attributeArr, "UIHXmlArrayAttributeAttribute");
string elementName = "";
if (attributeName.ElementName != null)
{
elementName = attributeName.ElementName;
}
else
{
elementName = propertyInfo.Name;
}
XmlElement xe = xDoc.CreateElement(elementName);
if (propertyInfo.PropertyType.IsGenericType)
{
object propertyValue = propertyInfo.GetValue(obj, null);
Type typeOfPropertyValue = propertyValue.GetType();
int numberOfListItem = Convert.ToInt32(typeOfPropertyValue.GetProperty("Count").GetValue(propertyValue, null));
for (int i = 0; i < numberOfListItem; i++)
{
object listItem = typeOfPropertyValue.GetProperty("Item").GetValue(propertyValue, new object[] { i });
xe = CreateXmlTree(listItem, xDoc, xe);
Node.AppendChild(xe);
}
}
else if (propertyInfo.PropertyType.IsClass)
{
object refObject = propertyInfo.GetValue(obj, null);
if (refObject != null)
{
Type objType = refObject.GetType();
xe = CreateXmlTree(refObject, xDoc, xe);
Node.AppendChild(xe);
}
}
}
#endregion
#region case 2
if (ExistAttribute(attributeArr, "UIHXmlAttributeAttribute") != null)
{
UIHXmlAttributeAttribute attributeName = (UIHXmlAttributeAttribute)ExistAttribute(attributeArr, "UIHXmlAttributeAttribute");
#region get element name
string elementName = "";
if (attributeName.AttributeName != null)
{
elementName = attributeName.AttributeName;
}
else
{
elementName = propertyInfo.Name;
}
#endregion
if (propertyInfo.GetValue(obj, null) != null)
{
#region get value of property
string propertyValue = "";
if (propertyInfo.PropertyType == typeof(TimeSpan))
{
DateTime start = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan timeSpan = (TimeSpan)propertyInfo.GetValue(obj, null);
DateTime ttt = start.Add(timeSpan);
propertyValue = ttt.ToString("yyyy/MM/dd-HH:mm:ss:fff");
}
else
{
propertyValue = propertyInfo.GetValue(obj, null).ToString();
}
#endregion
if (attributeName.DefaultValue == null)
{
XmlAttribute xe = xDoc.CreateAttribute(elementName);
xe.Value = propertyValue;
Node.Attributes.Append(xe);
}
else if (attributeName.DefaultValue != propertyValue)
{
XmlAttribute xe = xDoc.CreateAttribute(elementName);
xe.Value = propertyValue;
Node.Attributes.Append(xe);
}
}
}
#endregion
}
}
xElem.AppendChild(Node);
return xElem;
#endregion
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return null;
}
private static object ExistAttribute(object[] Attributes, string AttributeName)//找到特定的特性
{
for (int i = 0; i < Attributes.Length; ++i)
{
if (Attributes[i].GetType().Name == AttributeName)
{
return Attributes[i];
}
}
return null;
}
[Serializable]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Parameter |
AttributeTargets.Class | AttributeTargets.ReturnValue,AllowMultiple = false,Inherited = true)]
class UIHXmlAttributeAttribute:Attribute
{
public UIHXmlAttributeAttribute()
{
}
public UIHXmlAttributeAttribute(string elemName, string defaultValue)
{
AttributeName = elemName;
DefaultValue = defaultValue;
}
public string AttributeName { set; get; }
public string DefaultValue { set; get; }
}
[Serializable]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.ReturnValue | AttributeTargets.Class ,AllowMultiple = false)]
class UIHXmlArrayAttributeAttribute : Attribute
{
public UIHXmlArrayAttributeAttribute()
{
}
public UIHXmlArrayAttributeAttribute(string elemName,Type typeOfItem)
{
ElementName = elemName;
Type = typeOfItem;
}
public UIHXmlArrayAttributeAttribute(Type typeOfItem)
{
Type = typeOfItem;
}
public string ElementName {set;get;}
public Type Type { set; get; }
}
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法