缤纷多彩的植物信息世界

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

 

1、NameValue类的代码

   1:  using System.Xml.Serialization;
   2:   
   3:  /// <summary>
   4:  /// Summary description for NameValue
   5:  /// </summary>
   6:  /// 
   7:  [XmlRoot("NameValuePair")]
   8:  public class NameValue<KeyType, ValueType>
   9:  {
  10:      private KeyType _Key;
  11:      public KeyType key
  12:      {
  13:          get { return _Key; }
  14:          set
  15:          {
  16:              _Key = value;
  17:          }
  18:      }
  19:      private ValueType _Value;
  20:      public ValueType value
  21:      {
  22:          get { return _Value; }
  23:          set
  24:          {
  25:              _Value = value;
  26:          }
  27:      }
  28:       
  29:      public NameValue()
  30:      {
  31:          //
  32:          // TODO: Add constructor logic here
  33:          //
  34:      }
  35:  }

2、执行代码

   1:  using System;
   2:  using System.IO;
   3:  using System.Xml.Serialization;
   4:   
   5:  public partial class Default6 : System.Web.UI.Page
   6:  {
   7:      private string _xmlFilePath=@"c:\Data\NameValue.xml";
   8:      protected void Page_Load(object sender, EventArgs e)
   9:      {
  10:   
  11:      }
  12:      
  13:      public void Serialize(object sender, EventArgs e)
  14:      {
  15:          NameValue<int, string> nameVal = new NameValue<int, string>();
  16:          nameVal.key = 1;
  17:          nameVal.value = "加工厂";
  18:          XmlSerializer serializer = new XmlSerializer(typeof(NameValue<int, string>));
  19:          TextWriter writer = new StreamWriter(_xmlFilePath);
  20:          serializer.Serialize(writer, nameVal);
  21:          writer.Close();
  22:          lblResult.Text = "文件写入成功!"; 
  23:      }
  24:      public void Deserialize(object sender, EventArgs e)
  25:      {
  26:          XmlSerializer serializer = new XmlSerializer(typeof(NameValue<int,string>));
  27:          TextReader reader = new StreamReader(_xmlFilePath);
  28:          NameValue<int, string> nameVal = (NameValue<int, string>)serializer.Deserialize(reader);
  29:          reader.Close();
  30:          lblResult.Text = "键值:" + nameVal.key + "<br>";
  31:          lblResult.Text += "值: " + nameVal.value; 
  32:      }  
  33:  }

3、页面代码

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title></title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:      <asp:Button ID="btnSerialize" Text="串行化" runat="server" 
  13:              onclick="Serialize" />
  14:      <asp:Button ID="btnDeserialize" OnClick="Deserialize" Text="反串行化" runat="server" />
  15:      <br /><br />
  16:      <asp:Label ID="lblResult" runat="server" Height="21px" Width="351px"></asp:Label>
  17:      
  18:      </div>
  19:      </form>
  20:  </body>
  21:  </html>

4、XML文档和输出结果



posted on 2009-04-19 21:08  虎克  阅读(275)  评论(0)    收藏  举报