Asp.net XML心得4:写图片到xml

 1 protected void Page_Load(object sender, EventArgs e)
 2     {
 3         XmlWriterSettings settings = new XmlWriterSettings();
 4         settings.Indent = true;
 5         settings.IndentChars = "\t";
 6         settings.OmitXmlDeclaration = false;
 7 
 8         using (XmlWriter writer = XmlWriter.Create(Server.MapPath("WriteXmlImage.xml"), settings))
 9         {
10             string imgFileName = Server.MapPath("image/img01.jpg");
11 
12             writer.WriteStartDocument(false);
13             writer.WriteComment("This is my first image xml.");
14             writer.WriteStartElement("exployee"); writer.WriteAttributeString("id""1");
15                 writer.WriteStartElement("image"); writer.WriteAttributeString("filename", imgFileName);
16                     FileInfo fileInfo = new FileInfo(imgFileName);
17                     int size = (int)fileInfo.Length;
18                     byte[] imgBytes = new byte[size];
19                     FileStream imgFileStream = new FileStream(imgFileName, FileMode.Open);
20                     BinaryReader binaryReader = new BinaryReader(imgFileStream);
21                     imgBytes = binaryReader.ReadBytes(size);
22                     binaryReader.Close();
23 
24                     writer.WriteBinHex(imgBytes, 0, size);
25 
26                 writer.WriteEndElement();
27             writer.WriteEndElement();
28         }
29     }

 

输出结果:

 

1 <?xml version="1.0" encoding="utf-8" standalone="no"?>
2 <!--This is my first image xml.-->
3 <exployee id="1">
4     <image filename="E:\AspXml\image\img01.jpg">
5                FFD8FFE000104A4649460001..........
6         </image>
7 </exployee>

 

 

posted @ 2010-03-24 16:23  HelloCG  阅读(261)  评论(0编辑  收藏  举报