2012.6.12 .net自定义xml序列化及自定义特性
来源:互联网 发布:java 时间戳的长度 编辑:程序博客网 时间:2024/05/17 08:03
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Linq;using System.IO;using System.Xml.Serialization;using System.Reflection;namespace 自定义xml序列化及自定义特性{ class Program { static void Main(string[] args) { List<object> p = new List<object>(); Person pp = new Person(); pp.Age = 20; pp.Name = "xhc1"; p.Add(pp); Person ppp = new Person(); ppp.Name = "xhc2"; ppp.Age = 21; p.Add(ppp); Getxml(p); } static void Getxml(List<object> li) { XDocument doc = new XDocument(); XElement roott = new XElement("根节点"); doc.Add(roott); //不带自定义属性的序列化 foreach (object obj in li) { Type type = obj.GetType(); XElement root = new XElement(type.Name.ToString()); //获取属性,和值 foreach (PropertyInfo item in type.GetProperties()) { XElement xe = new XElement(item.Name.ToString(), item.GetValue(obj, null).ToString()); root.Add(xe); } roott.Add(root); } //带自定义属性的序列化 foreach (object obj in li) { Type type = obj.GetType(); XElement root = new XElement(type.Name.ToString()); //获取属性,和值 foreach (PropertyInfo item in type.GetProperties()) { //获取特性值; object[] o = item.GetCustomAttributes(typeof(ZdyarriAttribute),false); XElement xe = new XElement(item.Name.ToString(), item.GetValue(obj, null).ToString()); XAttribute xa = new XAttribute("中文",((ZdyarriAttribute)o[0]).DisZdyarri.ToString()); xe.Add(xa); root.Add(xe); } roott.Add(root); } doc.Save(@"C:\xx.xml"); } } public class Person { [Zdyarri("姓名")] public string Name { set; get; } [ZdyarriAttribute("年龄")] public int Age { set; get; } } public class ZdyarriAttribute : Attribute { public ZdyarriAttribute(string str) { DisZdyarri = str; } public string DisZdyarri { get; set; } }}
- 2012.6.12 .net自定义xml序列化及自定义特性
- .NET自定义序列化过程
- Newtonsoft.Json 自定义序列化特性
- .Net中的自定义特性
- 使用自定义属性实现Java和.net的XML序列化
- .net 序列化、反序列化之自定义序列化
- asp.net xml读取及序列化
- 序列化---自定义序列化
- 自定义序列化
- 自定义 序列化1
- 自定义 序列化2
- 自定义 序列化3
- 自定义序列化过程
- 自定义序列化
- Serializable:自定义序列化
- hadoop自定义序列化
- java 自定义序列化
- 自定义序列化
- 第15章(最终章) 写一个块设备驱动
- 一道很有典型的面试题目
- Java新建对象事情1
- ActivityManagerService中收集可用内存getMemoryInfo的实现
- spring-基于注释(Annotation)的配置
- 2012.6.12 .net自定义xml序列化及自定义特性
- 那些年,我们一起学的C++
- Android应用小技巧集锦
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- 易中天很有哲理的十句话
- SIP消息头域的说明
- 闲话四巨头:腾讯,阿里,百度,华为
- 读书笔记 Hacker‘s Delight
- SIP应答代码