DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> XML串行化數據的基礎
XML串行化數據的基礎
編輯:XML詳解     
XML文檔使用越來越普遍,我們常會把一些系統設置等文件用config或XML文檔存文在程序目錄下。.Net的串行化數據是一個很令人心動的技術,可以很方便地讀取一定格式或保存成文件。下面做個簡單了練習,對XML串行化作些初步了解。(注:我是在Windows應用程序上做的練習,因怕建立ASP.NetA工程麻煩,下面一大堆東西。其實除了路徑外,程序寫法基本一致)。

  先編寫一個需要串行化的類,該類是設置一個操作權限的設置文件

  using System;

  namespace Hellosea.WinModule.Account.Configuration
  {
      /// <summary>
      /// OperatePermissionSettings 的摘要說明。
      /// </summary>
      public class OperatePermissionSettings
      {
          public OperatePermissionSettings()
          { }

        public int SystemManage_Login;                    // 允許進入管理頁面,為各管理角色所擁用的許可
 public int SystemManage_SystemSetting;        // 可以進行系統參數、安全等設置的許可

        public int UserManage_Enter;                    // 進入用戶管理頁面的許可
        public int UserManage_PurvIEwAssign;        // 可以分配或收回用戶角色、許可等的權限
        public int UserManage_UserDelete;            // 可以中止用戶帳號的許可
 public int UserManage_UserEdit;                // 可以編輯用戶信息的許可
      }
  }


  串行化該類,並保存文件:

  private void button1_Click(object sender, System.EventArgs e)
  {
      // 建立操作權限類
      OperatePermissionSettings settings = new OperatePermissionSettings();
      settings.SystemManage_Login = 1;
      settings.SystemManage_SystemSetting = 2;
      settings.UserManage_Enter = 3;
      settings.UserManage_PurvIEwAssign = 4;
settings.UserManage_UserEdit = 5;
      settings.UserManage_UserDelete = 6;

      // 文件流
      string filePath = Application.StartupPath + "\\OperatePermission.Config";
      FileStream fs = new FileStream(filePath, FileMode.Create);

      // 串行化數據
      XmlSerializer serializer = new XMLSerializer(typeof(OperatePermissionSettings));
      serializer.Serialize(fs, settings);

      // 關閉文件流
      fs.Close();
  }


  反串行化,從文件中讀取數據

  private void button2_Click(object sender, System.EventArgs e)
  {
      OperatePermissionSettings settings = new OperatePermissionSettings();
// 文件流
      string filePath = Application.StartupPath + "\\OperatePermission.Config";
      FileStream fs = new FileStream(filePath, FileMode.Open);

      // 反串行化數據
      XmlSerializer serializer = new XMLSerializer(typeof(OperatePermissionSettings));
      settings = (OperatePermissionSettings)serializer.Deserialize(fs);

            
      // 關閉文件流
      fs.Close();

      textBox1.Text = settings.SystemManage_Login.ToString();
textBox2.Text = settings.SystemManage_SystemSetting.ToString();
      textBox3.Text = settings.UserManage_Enter.ToString();
      textBox4.Text = settings.UserManage_PurvIEwAssign.ToString();
      textBox5.Text = settings.UserManage_UserEdit.ToString();
      textBox6.Text = settings.UserManage_UserDelete.ToString();
  }


  當然需要用到二個命名空間
  using System.IO;
  using System.XML.Serialization;

  另外OperatePermission.Config文檔的結構如下:
  <?XML version="1.0"?>
  <OperatePermissionSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SystemManage_Login>1</SystemManage_Login>
      <SystemManage_SystemSetting>2</SystemManage_SystemSetting>
 <UserManage_Enter>3</UserManage_Enter>
      <UserManage_PurviewAssign>4</UserManage_PurvIEwAssign>
      <UserManage_UserDelete>6</UserManage_UserDelete>
      <UserManage_UserEdit>5</UserManage_UserEdit>
  </OperatePermissionSettings>

  這是最簡單的串行化,只作個范例。真正實現時,最好能對該方法用類包裝起來,使用起來會有非常好的可讀性。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved