zoukankan      html  css  js  c++  java
  • 将对象序列化为XML文档

        如今在面向对象和XML盛行的时代,能够很好的发挥二者的特长,并协调合作,未来的网络结构和网络开发的模式将会有质的改变。网络开发将逐步的走向系统化、模块化、精尖化。所以呢,能够很好的运用好这些新的东西对于我们从事网络开发的人很重要。这是一个简单的小例子,实现了对象转化为XML。如果欠缺之处,希望大家批评指正。
    前台调用:
     1using System;
     2using Bmc;
     3
     4public partial class _Default : System.Web.UI.Page 
     5{
     6    protected void btnSerializer_Click(object sender, EventArgs e)
     7    {
     8        Comment firstComment = new Comment();
     9        firstComment.ID = 1;
    10        firstComment.userName = "first";
    11        Comment secondComment = new Comment();
    12        secondComment.userName = "second";
    13        secondComment.ID = 2;
    14
    15        CommentArray commentArray = new CommentArray();
    16        commentArray.Set(firstComment);
    17        commentArray.Set(secondComment);
    18
    19        System.Type type = typeof(CommentArray);
    20        SerializaObject.Serializa(Server.MapPath("~/1.XML"), type, commentArray);
    21    }

    22}

    23
    具体操作类:
     1using System;
     2using System.Data;
     3using System.Configuration;
     4using System.Web;
     5using System.Web.Security;
     6using System.Web.UI;
     7using System.Web.UI.WebControls;
     8using System.Web.UI.WebControls.WebParts;
     9using System.Web.UI.HtmlControls;
    10using System.IO;
    11using System.Xml.Serialization;
    12
    13/// <summary>
    14/// Summary description for Serializer
    15/// </summary>

    16public class SerializaObject
    17{
    18    public SerializaObject()
    19    {
    20        //
    21        // TODO: Add constructor logic here
    22        //
    23    }

    24    /// <summary>
    25    /// serilia
    26    /// </summary>
    27    /// <param name="filePath"></param>
    28    /// <param name="objectType"></param>
    29    /// <param name="nObject"></param>
    30    /// <returns></returns>

    31    public static bool Serializa(string filePath,Type objectType,object nObject)
    32    {
    33        FileStream fileStream = new FileStream(@filePath, FileMode.Create);
    34        XmlSerializer serializer = new XmlSerializer(objectType);
    35        try
    36        {
    37            serializer.Serialize(fileStream, nObject);
    38            return true;
    39        }

    40        catch(Exception ex)
    41        {  
    42           throw ex;
    43        }

    44        finally
    45        {
    46            fileStream.Close();
    47        }
        
    48    }

    49}

    50

    相关的实体类:
     1using System;
     2using System.Data;
     3using System.Configuration;
     4using System.Collections;
     5using System.Collections.Generic;
     6
     7namespace Bmc
     8{
     9    /// <summary>
    10    /// 评论Comment类,主要方法,可以实现Comment对象的:添加、删除、查看
    11    /// </summary>

    12    [Serializable]
    13    public class Comment
    14    {
    15        /// <summary>
    16        /// Global_ID
    17        /// </summary>   

    18        public int ID;
    19        /// <summary>
    20        /// 用户名或者昵称
    21        /// </summary>

    22        public string userName;
    23        /// <summary>
    24        /// 编辑时间
    25        /// </summary>

    26        public DateTime EditTime;
    27        /// <summary>
    28        /// 状态
    29        /// </summary>

    30        public int states;
    31        /// <summary>
    32        /// Normal
    33        /// </summary>

    34        public const int StatusNormal = 1;
    35        /// <summary>
    36        /// Deleted
    37        /// </summary>

    38        public const int StatusDeleted = 0;
    39        /// <summary>
    40        /// Table Name
    41        /// </summary>

    42        public const string tableName = "T_Comment";
    43        /// <summary>
    44        /// 初始化
    45        /// </summary>

    46        public Comment()
    47        {
    48            userName = "游客";
    49            states = StatusNormal;
    50            EditTime = System.DateTime.Now;
    51        }

    52    }

    53    [Serializable]
    54    public class CommentArray
    55    {
    56        /// <summary>
    57        /// Comment Array
    58        /// </summary>

    59        public System.Collections.Generic.List<Comment> iArray;
    60        /// <summary>
    61        /// Initial
    62        /// </summary>

    63        public CommentArray()
    64        {
    65            iArray = new List<Comment>();
    66        }

    67        /// <summary>
    68        /// Set Value
    69        /// </summary>
    70        /// <param name="iComment"></param>

    71        public void Set(Comment iComment)
    72        {
    73            iArray.Add(iComment);
    74        }

    75        /// <summary>
    76        /// Remove Value From Comment Array
    77        /// </summary>

    78        public void Remove(Comment iComment)
    79        {
    80            iArray.Remove(iComment);
    81        }

    82    }

    83
    84}
    运行结果: 注意:最开始的XML说明缺少了encoding="utf-8"?
     1<?xml version="1.0"?>
     2<CommentArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     3  <iArray>
     4    <Comment>
     5      <ID>1</ID>
     6      <userName>first</userName>
     7      <EditTime>2007-11-07T18:27:33.15625+08:00</EditTime>
     8      <states>1</states>
     9    </Comment>
    10    <Comment>
    11      <ID>2</ID>
    12      <userName>second</userName>
    13      <EditTime>2007-11-07T18:27:33.15625+08:00</EditTime>
    14      <states>1</states>
    15    </Comment>
    16  </iArray>
    17</CommentArray
    改进办法:使用指定的 TextWriter 序列化指定的 Object 并将 XML 文档写入文件。
     1    /// <summary>
     2    /// Serializa,这个加入了xml的编码规则UTF8
     3    /// </summary>
     4    /// <param name="filePath"></param>
     5    /// <param name="objectType"></param>
     6    /// <param name="nObject"></param>
     7    /// <returns></returns>

     8    public static bool Serializa(string filePath, Type objectType, object nObject)
     9    {
    10        FileStream fileStream = new FileStream(@filePath, FileMode.Create);
    11        TextWriter writer = new StreamWriter(fileStream, new UTF8Encoding());
    12        XmlSerializer serializer = new XmlSerializer(objectType);
    13        try
    14        {
    15            serializer.Serialize(writer, nObject);
    16            return true;
    17        }

    18        catch (Exception ex)
    19        {
    20            throw ex;
    21        }

    22        finally
    23        {
    24            writer.Close();
    25        }

    26    }

    改进后的结果:

     1<?xml version="1.0" encoding="utf-8"?>
     2<CommentArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     3  <iArray>
     4    <Comment>
     5      <ID>1</ID>
     6      <userName>first</userName>
     7      <EditTime>2007-11-07T18:55:41.9375+08:00</EditTime>
     8      <states>1</states>
     9    </Comment>
    10    <Comment>
    11      <ID>2</ID>
    12      <userName>second</userName>
    13      <EditTime>2007-11-07T18:55:41.9375+08:00</EditTime>
    14      <states>1</states>
    15    </Comment>
    16  </iArray>
    17</CommentArray>
  • 相关阅读:
    数学建模(一)层次分析法
    数学建模(二)优劣解距离法Topsis模型部分
    python语法学习第十一天--模块
    机器学习——交叉验证,GridSearchCV,岭回归
    机器学习——logistic回归,鸢尾花数据集预测,数据可视化
    机器学习入门——线性回归预测广告投入数据集
    凸优化,对偶问题与拉格朗日函数
    机器学习中的凸优化,凸集,凸函数的相关定义和理论
    调整数组顺序使奇数位于偶数前面
    leetcode| 329. 矩阵中的最长递增路径
  • 原文地址:https://www.cnblogs.com/yank/p/952465.html
Copyright © 2011-2022 走看看