页面代码:
<%@ 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: }