zoukankan      html  css  js  c++  java
  • WP7中对本地XML文件的序列化与反序列化

     

    1、两个用于序列化和反序列化的类  

     public class Family
        {
            public string FamilyName { get; set; }
            public int FamilyNum { get; set; }

        }

     public class Student
        {
            public int StuID { get; set; }

            public string StuName { get; set; }

            public string StuSchool { get; set; }

            public Family Fa { get; set; }    //属性Fa是Family类的对象
        }

    2.XMLFile.Xml 文件的内容

    <?xml version="1.0"?>
    <ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Student>
        <StuID>1</StuID>
        <StuName>曹伟</StuName>
        <StuSchool>思远IT</StuSchool>
        <Fa>
          <FamilyName>FamilyOne</FamilyName>
          <FamilyNum>3</FamilyNum>
        </Fa>
      </Student>
      <Student>
        <StuID>2</StuID>
        <StuName>雷敏</StuName>
        <StuSchool>义宏小学</StuSchool>
        <Fa>
          <FamilyName>FamilyTwo</FamilyName>
          <FamilyNum>5</FamilyNum>
        </Fa>
      </Student>
      <Student>
        <StuID>3</StuID>
        <StuName>陈龙</StuName>
        <StuSchool>车胤中学</StuSchool>
        <Fa>
          <FamilyName>FamilyThree</FamilyName>
          <FamilyNum>6</FamilyNum>
        </Fa>
      </Student>
    </ArrayOfStudent> 

    3、PubXmlSerialize.cs

     public class PubXmlSerialize
        {
            /// <summary>
            /// 对象序列化成 XML String
            /// </summary>
            public static string XmlSerialize<T>(T obj)
            {
                string xmlString = string.Empty;
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream())
                {
                    xmlSerializer.Serialize(ms, obj);
                    ms.Position = 0;
                    xmlString = new StreamReader(ms).ReadToEnd();

                }
                return xmlString;
            }

            /// <summary>
            /// XML String 反序列化成对象
            /// </summary>
            public static T XmlDeserialize<T>(string xmlString)
            {
                T t = default(T);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
                {
                    using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                    {
                        Object obj = xmlSerializer.Deserialize(xmlReader);
                        t = (T)obj;
                    }
                }
                return t;
            }
        }

    一、一般对象集的序列化与反序列化

     List<Student> sss = new List<Student>
                    {
                      new Student{StuID=1,StuName="张三",StuSchool="school1",Fa=new Family(){FamilyName="name1",FamilyNum=3}},
                      new Student{StuID=1,StuName="李四",StuSchool="school2",Fa=new Family(){FamilyName="name2",FamilyNum=4}}

                    };

                string strXml = PubXmlSerialize.XmlSerialize<List<Student>>(sss);                           //序列化
                List<Student> students = PubXmlSerialize.XmlDeserialize<List<Student>>(strXml);  //反序列化

    二、Windows Phone7中对本地xml文件的操作

                注:获得本地xml文件学将xml文件的属性"建置动作"设置为Resource

                Stream  str = App.GetResourceStream(new Uri("/PhoneAppSerivable;component/XMLFile.xml", UriKind.Relative)).Stream;
                XDocument doc = XDocument.Load(str);    //加载本地xml文件

            
                List<Student> students = PubXmlSerialize.XmlDeserialize<List<Student>>(doc.ToString());   //反序列化为对象
             

    三、使用linq对xml文件进行遍历
                var v = from t in doc.Descendants("Student")
                        select new Student
                        {
                            StuID = int.Parse(t.Element("StuID").Value),
                            StuName = t.Element("StuName").Value,
                            StuSchool = t.Element("StuSchool").Value,
                            Fa = new Family
                            {
                                FamilyName = t.Element("Fa").Element("FamilyName").Value,
                                FamilyNum = int.Parse(t.Element("Fa").Element("FamilyNum").Value),
                            }
                        };
                students = v.ToList();

    ****:如果XML文件中引用了命名空间,则会出现“xml文件格式不正确”的错误,我们需要得到xml后将命名空间用Replace函数替换为空即可。

    http://www.xnwai.com/2012/06/windowsphone-serialization-xml-json.html

  • 相关阅读:
    Linux基础-shell脚本知识整理和脚本编写----------变量、运算符、流程控制、函数、计划任务(发送邮件)
    Linux基础-正则表达式整理---------------grep、sed、awk
    Linux基础-配置网络、集群内主机名设定、ssh登入、bash命令、通配符(元字符)
    Linux基础-----------nginx安装和nginx web、nginx反向代理、nfs 服务
    Linux基础--------监控系统、进程管理、软件包管理-------free、dd、kill、 rpm、yum、源码安装python
    Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接
    Linux用户创建及权限管理
    django博客项目6:Django Admin 后台发布文章
    django博客项目5:博客首页视图(2)
    django博客项目4:博客首页视图(1)
  • 原文地址:https://www.cnblogs.com/androllen/p/2770729.html
Copyright © 2011-2022 走看看