序列化特性测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.Collections;

namespace 序列化特性测试
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //XmlSerializer cap = new XmlSerializer(typeof(Globals));
            //Globals global = new Globals();
            //cap.Serialize(Console.Out, global);
            //Console.WriteLine("\n");

            //获取Globals的字段上的特性名称
            List<string> namelist = new List<string>();
            var datalist = new ArrayList();

            Globals obj = new Globals();
            Type type1 = obj.GetType();
            //获取xml对象中结构的名字和数据
            var arraylist = new ArrayList();
            List<int> len = new List<int>();
            int lenindex = -1;
            foreach (FieldInfo info in type1.GetFields())
            {
                foreach (Attribute node in info.GetCustomAttributes(true))
                {
                    //特性XmlElementAttribute
                    if (node.GetType().ToString() == "System.Xml.Serialization.XmlElementAttribute")
                    {
                        XmlElementAttribute enode = (XmlElementAttribute)node;

                        if (info.GetValue(obj) is Array)
                        {
                            if (info.GetValue(obj).GetType().ToString() == "System.String[]")
                            {
                                string[] data = (string[])info.GetValue(obj);
                                for (int i = 0; i < data.Length; i++)
                                {
                                    datalist.Add(data[i]);
                                    namelist.Add(enode.ElementName);
                                }
                            }
                            else if (info.GetValue(obj).GetType().ToString() == "System.Int32[]")
                            {
                                int[] data = (int[])info.GetValue(obj);
                                for (int i = 0; i < data.Length; i++)
                                {
                                    datalist.Add(data[i]);
                                    namelist.Add(enode.ElementName);
                                }

                            }
                            else
                            {
                                ;
                            }

                        }

                        else //不是数组情况
                        {
                            namelist.Add(enode.ElementName);
                            var innertext = info.GetValue(obj);

                            if (innertext.GetType().ToString() == "System.Boolean")
                            {
                                datalist.Add(innertext.ToString().ToLower());
                            }
                            else
                            {
                                datalist.Add(innertext);
                            }

                        }

                    }

                    else if (node.GetType().ToString() == "System.Xml.Serialization.XmlArrayAttribute")
                    {
                        string[] array = (string[])info.GetValue(obj);
                        XmlArrayAttribute anode = (XmlArrayAttribute)node;
                        namelist.Add(anode.ElementName);
                        datalist.Add(array);
                        len.Add(array.Length);
                        for (int i = 0; i < array.Length; i++)
                        {
                            arraylist.Add(array[i]);
                        }
                    }
                }
            }

                //打印名字和数据
            for (int i=0;i<namelist.Count;i++)
            {
                Console.WriteLine(namelist[i]);
                if (datalist[i] is Array)
                {
                    lenindex++;
                    for (int j = 0; j <len[lenindex]; j++)
                    {
                        Console.WriteLine(arraylist[j]);
                    }
                }
                else
                {
                    Console.WriteLine(datalist[i]);
                }

            }




        }
    }

        [XmlRoot("学生")]
        public class Globals
        {
            [XmlElement("姓名")]
            public string name = "A";

            [XmlElement("年龄")]
            public int age = 18;

            [XmlArray("科目")]
            public string[] subject = new string[] { "语文", "数学", "英语" };

            [XmlElement("科目成绩")]
            public string[] name1 = { "语文91", "数学97","英语94"};

            [XmlElement("是否录取")]
            public bool isadmit = true;

        }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值