不少时候,大家开辟顺序都亟待选拔到对象的XML类别化和反连串化,对象的XML种类化和反连串化,既可以应用XML对象(XmlDocument
)实行操作,也得以利用XmlSerializer实行操作,多个各有帮助和益处,就操作性而已,笔者扶助于选拔后者来操作XML。本文介绍两种办法操作XML,普通的XDocument的API操作,方便的XmlSerializer对象种类化及反连串化操作,加密XML对象系列化操作。

一、普通的XML对象操作,利用XML对象(XmlDocument )。

二个头名的行使XmlDocument对象操作XML的例子代码如下。

    public static void RunSnippet()
    {
        XmlDocument xmldoc = new XmlDocument ( ) ;
    //加入XML的声明段落
    XmlNode xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;
    xmldoc.AppendChild (xmlnode ) ;
    //加入一个根元素
    XmlElement xmlelem = xmldoc.CreateElement ( "" , "ROOT" , "" ) ;
    XmlText xmltext = xmldoc.CreateTextNode ( "Root Text" ) ;
    xmlelem.AppendChild ( xmltext ) ;
    xmldoc.AppendChild ( xmlelem ) ;
    //加入另外一个元素
    XmlElement xmlelem2 = xmldoc.CreateElement ("SampleElement" ) ;
    xmlelem2 = xmldoc.CreateElement ( "" , "SampleElement" , "" ) ;
    xmltext = xmldoc.CreateTextNode ( "The text of the sample element" ) ;
    xmlelem2.AppendChild ( xmltext ) ;
    xmldoc.ChildNodes.Item(1).AppendChild ( xmlelem2 ) ;
    //保存创建好的XML文档
    try
    {
      xmldoc.Save ( "c:\\data.xml" ) ;
    }
    catch ( Exception e )
    {
      //显示错误信息
      Console.WriteLine ( e.Message ) ;
    }
    Console.ReadLine ( ) ;
    }

获得的出口结果如下所示。

<?xml version="1.0"?>
<ROOT>Root Text
    <SampleElement>The text of the sample element</SampleElement>
</ROOT>

2、使用XmlSerializer进行XML操作

先提供四个目的的系列化和反连串化的封装函数,如下所示。

        /// <summary>
        /// 对象序列化XML到文件中
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="obj">对象</param>
        /// <param name="type">对象类型</param>
        private bool XmlSerialize(string path, object obj, Type type)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            try
            {
                //如果文件目录不存在,那么创建目录
                if (!File.Exists(path))
                {
                    FileInfo fi = new FileInfo(path);
                    if (!fi.Directory.Exists)
                    {
                        Directory.CreateDirectory(fi.Directory.FullName);
                    }
                }

                using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    XmlSerializer format = new XmlSerializer(type);

                    format.Serialize(stream, obj, ns);
                    stream.Close();
                }

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        private object XmlDeserialize(string path, Type type)
        {
            try
            {
                using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XmlSerializer formatter = new XmlSerializer(type);
                    stream.Seek(0, SeekOrigin.Begin);
                    object obj = formatter.Deserialize(stream);
                    stream.Close();
                    return obj;
                }
            }
            catch
            {
                return null;
            }
        }

专注个中代码       

 

     XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

     ns.Add("", "");

是把默许的xmlns命名空间多余的内容去掉,那样得到比较干净的XML。

在行使的时候,大家必要定义好对象的实体,那样本领加之对象开始展览操作,定义的实体类如下所示。

    [Serializable]
    public class UserInfo
    {                
        public string ID { get; set; }
        public string UserNo{ get; set; }
        public string UserName { get; set; }
        public string Sex { get; set; }
        public string Birthday { get; set; }
    }

    [Serializable]
    public class DataOfUser
    {
        [XmlElement(ElementName = "m_User")]
        public List<UserInfo> list = new List<UserInfo>();
    }

调用种类化函数的代码例子如下所示

       private void btnUserNormal_Click(object sender, EventArgs e)
        {
            DataOfUser obj = new DataOfUser();
            UserInfo user = new UserInfo();
            user.ID=Guid.NewGuid().ToString();
            user.Sex = "男";
            user.UserName = "张三";
            user.UserNo = "20010001";
            user.Birthday = "1999-1-1";
            obj.list.Add(user);

            user = new UserInfo();
            user.ID = Guid.NewGuid().ToString();
            user.Sex = "女";
            user.UserName = "李氏";
            user.UserNo = "20020001";
            user.Birthday = "1998-1-1";
            obj.list.Add(user);

            try
            {
                XmlSerialize("C:\\User.xml", obj, obj.GetType());
                MessageUtil.ShowTips("Ok");
            }
            catch (Exception ex)
            {
                MessageUtil.ShowError(ex.Message);
            }
        }

如此得到的XML内容如下所示。

<?xml version="1.0"?>
<DataOfUser>
  <m_User>
    <ID>f8a8b323-5c56-4c21-9ddf-1cd30f78dfca</ID>
    <UserNo>20010001</UserNo>
    <UserName>张三</UserName>
    <Sex>男</Sex>
    <Birthday>1999-1-1</Birthday>
  </m_User>
  <m_User>
    <ID>bbb36378-ec27-4e20-ad4b-2d2dc7e142e4</ID>
    <UserNo>20020001</UserNo>
    <UserName>李氏</UserName>
    <Sex>女</Sex>
    <Birthday>1998-1-1</Birthday>
  </m_User>
</DataOfUser>

反连串化的操作也相比较轻巧,不再赘述。

叁)把目标实例化到XML中并开始展览加密管理

 首先大家定义三个连串化加密、解码并反类别化的函数如下所示。

        /// <summary>
        /// XML序列化并加密
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="obj">对象</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        private bool XmlSerializeEncrypt(string path, object obj, Type type)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            try
            {
                if (!File.Exists(path))
                {
                    FileInfo fi = new FileInfo(path);
                    if (!fi.Directory.Exists)
                    {
                        Directory.CreateDirectory(fi.Directory.FullName);
                    }
                }

                using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {       
                    string content = "";
                    using(MemoryStream ms = new MemoryStream())
                    {
                        XmlSerializer format = new XmlSerializer(type);
                        format.Serialize(ms, obj, ns);
                        ms.Seek(0, 0);
                        content = Encoding.UTF8.GetString(ms.ToArray());
                    }

                    string encrypt = EncodeHelper.EncryptString(content);
                    byte[] bytes = Encoding.UTF8.GetBytes(encrypt);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                }


                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 解密并进行XML反序列化
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        private object XmlDeserializeDecrypt(string path, Type type)
        {
            try
            {
                string encrypt = File.ReadAllText(path, Encoding.UTF8);
                string content = EncodeHelper.DecryptString(encrypt, true);
                byte[] bytes = Encoding.UTF8.GetBytes(content);
                using (MemoryStream stream = new MemoryStream(bytes))
                {                  
                    XmlSerializer formatter = new XmlSerializer(type);

                    stream.Seek(0, SeekOrigin.Begin);
                    object obj = formatter.Deserialize(stream);
                    stream.Close();
                    return obj;
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

这么函数定义好后,调用和眼下未有加密的大都,如下所示。

        private void btnUserEncrypt_Click(object sender, EventArgs e)
        {
            DataOfUser obj = new DataOfUser();
            UserInfo user = new UserInfo();
            user.ID = Guid.NewGuid().ToString();
            user.Sex = "男";
            user.UserName = "张三";
            user.UserNo = "20010001";
            user.Birthday = "1999-1-1";
            obj.list.Add(user);

            user = new UserInfo();
            user.ID = Guid.NewGuid().ToString();
            user.Sex = "女";
            user.UserName = "李氏";
            user.UserNo = "20020001";
            user.Birthday = "1998-1-1";
            obj.list.Add(user);

            try
            {
                XmlSerializeEncrypt("C:\\User-Encrypt.xml", obj, obj.GetType());
                MessageUtil.ShowTips("Ok");
            }
            catch (Exception ex)
            {
                MessageUtil.ShowError(ex.Message);
            }
        }

        private void btnUserDecrypt_Click(object sender, EventArgs e)
        {
            string file = FileDialogHelper.OpenFile();
            if (!string.IsNullOrEmpty(file))
            {
                DataOfUser info = XmlDeserializeDecrypt(file, typeof(DataOfUser)) as DataOfUser;
                if (info != null)
                {
                    MessageUtil.ShowTips("OK");
                }
            }
        }

 

相关文章

网站地图xml地图