XML学习笔记(五):读写非文本格式的XML文件

页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NonTextDataXML.aspx.cs" Inherits="NonTextDataXML" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Image file name:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        Target XML file name:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Save images as XML" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Validation Output" OnClick="Button2_Click" />
        <br />
        <asp:ImageButton ID="ImageButton1" runat="server" Height="230px" Width="155px" />
    </div>
    </form>
</body>
</html>

执行代码:

   1:  using System;
   2:  using System.Web.UI.WebControls;
   3:  using System.Xml;
   4:  using System.IO;
   5:   
   6:   
   7:   
   8:  public partial class NonTextDataXML : System.Web.UI.Page
   9:  {
  10:      protected void Page_Load(object sender, EventArgs e)
  11:      {
  12:   
  13:      }
  14:      /// <summary>
  15:      /// Handles the Click event of the Button1 control.
  16:      /// </summary>将图片文件存储为XML文件格式(串行化)
  17:      /// The code creates an XmlTextWriter object by passing the path of the destination XML file to the constructor. 
  18:      /// Then a FileStream is created for reading data from the image file. The contents of the file are read by using the Read() method of the FileStream class, which accepts three parameters: the byte array to read the data into, the start index in the byte array from where the writing should start, and the length of data to read. 
  19:      /// The XmlTextWriter then starts writing the document. It first writes the XML processing instruction and the <imagefile> element. The <imagefile> element has two attributes: filename and size. The filename attribute stores the complete path of the image file that is being serialized as XML. The size attribute contains the size of the source image file.Image files contain nontextual data. 
  20:      /// we use Base64 encoding. To write data into Base64 format, the XmlTextWriter class provides a method called WriteBase64(), which accepts three parameters: a byte array that contains the nontextual data, the index of the byte array from which the writing should start, and the length of data to write. The WriteBase64() method writes the supplied byte array as a Base64 string inside the destination XML element. 
  21:      /// <param name="sender">The source of the event.</param>
  22:      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  23:      protected void Button1_Click(object sender, EventArgs e)
  24:      {
  25:          XmlTextWriter writer = new XmlTextWriter(TextBox2.Text, null);
  26:          FileStream fs = File.OpenRead(TextBox1.Text);
  27:          byte[] data = new byte[fs.Length];
  28:          fs.Position = 0;
  29:          fs.Read(data, 0, data.Length);
  30:          fs.Close();
  31:          writer.WriteStartDocument();
  32:          writer.WriteStartElement("imagefile");
  33:          writer.WriteAttributeString("filename", TextBox1.Text);
  34:          writer.WriteAttributeString("size", data.Length.ToString());
  35:          writer.WriteBase64(data, 0, data.Length);
  36:          writer.WriteEndElement();
  37:          writer.Close();
  38:      }
  39:      /// <summary>
  40:      /// Handles the Click event of the Button2 control.
  41:      /// </summary>把以XML文件格式存储的图片文件显示为图片(反串行化)
  42:      /// The code creates an instance of the XmlTextReader class by passing the XML document we just created. It then starts reading the document. If the element name is imagefile, the code reads the two attributes filename and size. Based on the value of the size attribute, a byte array is created with that much capacity. The contents of the <imagefile> element are read by using the ReadElementString() method.
  43:      /// <param name="sender">The source of the event.</param>
  44:      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  45:      protected void Button2_Click(object sender, EventArgs e)
  46:      {
  47:          XmlTextReader reader = new XmlTextReader(TextBox2.Text);
  48:          reader.WhitespaceHandling = WhitespaceHandling.None;
  49:          while (reader.Read())
  50:          {
  51:              if (reader.NodeType == XmlNodeType.Element)
  52:              {
  53:                  if (reader.Name == "imagefile")
  54:                  {
  55:                      int length = int.Parse(reader.GetAttribute("size"));
  56:                      string filename = reader.GetAttribute("filename");
  57:                      byte[] data = new byte[length];
  58:                      string str = reader.ReadElementString();
  59:                      byte[] imagedata = Convert.FromBase64String(str);
  60:                      MemoryStream ms = new MemoryStream();
  61:                      ms.Write(imagedata, 0, imagedata.Length);
  62:                      Image image = Image.FromStream(ms);
  63:                      pictureBox1.Image = image;
  64:                      ms.Close();
  65:                  }
  66:              }
  67:          }
  68:      }
  69:  }

转载于:https://www.cnblogs.com/apiaceae/archive/2009/05/09/1453291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值