ASP.NET中常用功能代码总结(4)——保存图片到XML文件

本文介绍了一种使用XML文件来存储图片数据的方法,并提供了如何将图片数据写入XML文件及从XML文件中读取图片数据的具体实现步骤。

一.保存图片到XML文件<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 2InBlock.gif/// 保存图片到XML文件
 3ExpandedBlockEnd.gif/// </summary>

 4None.gifprivate void UploadImageToXml()
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////得到用户要上传的文件名
 7InBlock.gif    string strFilePathName = loFile.PostedFile.FileName;
 8InBlock.gif    string strFileName = Path.GetFileName(strFilePathName);
 9InBlock.gif    int FileLength = loFile.PostedFile.ContentLength;
10InBlock.gif    if(FileLength<=0)
11InBlock.gif        return;
12InBlock.gif    try
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{    
14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////图象文件临时储存Byte数组
15InBlock.gif        Byte[] FileByteArray = new Byte[FileLength]; 
16InBlock.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////建立数据流对像
18InBlock.gif        Stream StreamObject = loFile.PostedFile.InputStream; 
19InBlock.gif
20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
21InBlock.gif        StreamObject.Read(FileByteArray,0,FileLength); 
22InBlock.gif
23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////要打开的文件
24InBlock.gif        string fileName = Server.MapPath(".\\WriteXml.xml");   
25InBlock.gif    
26InBlock.gif        XmlDocument xmlDoc = new XmlDocument();
27InBlock.gif        xmlDoc.Load(fileName);
28InBlock.gif
29ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////查找<dbGuest>
30InBlock.gif        XmlNode root=xmlDoc.SelectSingleNode("dbImage");
31InBlock.gif        XmlNodeList xnl=xmlDoc.SelectSingleNode("dbImage").ChildNodes;
32InBlock.gif        int nIndex = xnl.Count;
33InBlock.gif
34ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////以下添加新结点
35InBlock.gif        XmlElement xe1=xmlDoc.CreateElement("Image");//创建一个<User>节点
36InBlock.gif
37InBlock.gif        XmlElement xesub1=xmlDoc.CreateElement("ImageID");
38InBlock.gif        
39ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////设置文本节点
40InBlock.gif        xesub1.InnerText=nIndex.ToString();
41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////添加到<User>节点中
43InBlock.gif        xe1.AppendChild(xesub1);
44InBlock.gif        XmlElement xesub2=xmlDoc.CreateElement("ImageContentType");
45InBlock.gif        xesub2.InnerText=loFile.PostedFile.ContentType;
46InBlock.gif        xe1.AppendChild(xesub2);
47InBlock.gif        XmlElement xesub3=xmlDoc.CreateElement("ImageSize");
48InBlock.gif        xesub3.InnerText=FileLength.ToString();
49InBlock.gif        xe1.AppendChild(xesub3);
50InBlock.gif        XmlElement xesub4=xmlDoc.CreateElement("ImageDescription");
51InBlock.gif        xesub4.InnerText=tbDescription.Text;
52InBlock.gif        xe1.AppendChild(xesub4);
53InBlock.gif        XmlElement xesub5=xmlDoc.CreateElement("ImageData");
54InBlock.gif        xesub5.InnerText= Convert.ToBase64String(FileByteArray);
55InBlock.gif        xe1.AppendChild(xesub5);
56InBlock.gif        
57ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////添加到<dbGuest>节点中
58InBlock.gif        root.AppendChild(xe1);
59InBlock.gif        xmlDoc.Save(fileName);
60InBlock.gif    
61InBlock.gif        Response.Redirect("ShowAllImg.aspx");
62ExpandedSubBlockEnd.gif    }

63InBlock.gif    catch(Exception ex)
64ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
65InBlock.gif        throw ex;
66ExpandedSubBlockEnd.gif    }

67ExpandedBlockEnd.gif}

二.从XML中读取图片数据

 1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 2InBlock.gif/// 从XML中读取图片
 3InBlock.gif/// </summary>
 4ExpandedBlockEnd.gif/// <param name="ImageID">图片ID</param>

 5None.gifprivate void ReadImageFromXml(string ImageID)
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{    
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////ID为图片ID
 8InBlock.gif    int ImgID = Convert.ToInt32(ImageID); 
 9InBlock.gif
10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**////要打开的文件
11InBlock.gif    string fileName = Server.MapPath(".\\WriteXml.xml");  
12InBlock.gif    
13InBlock.gif    XmlDocument xmlDoc = new XmlDocument();
14InBlock.gif    xmlDoc.Load(fileName);
15InBlock.gif    XmlNodeList node =  xmlDoc.SelectSingleNode("//Image[ImageID='"+ImgID.ToString()+"']").ChildNodes;
16InBlock.gif    if(node!=null)
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif        string strType = node.Item(1).InnerText;
19InBlock.gif        string strData =node.Item(4).InnerText;
20InBlock.gif        int nSize = int.Parse(node.Item(2).InnerText);
21InBlock.gif        
22ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////设定输出文件类型
23InBlock.gif        Response.ContentType = strType;
24InBlock.gif
25ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////输出图象文件二进制数制
26InBlock.gif        Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize); 
27InBlock.gif        Response.End();
28InBlock.gif
29InBlock.gif        //也可以保存为图像
30InBlock.gif        //FileStream fs = new FileStream(@"C:\aa.BMP", FileMode.OpenOrCreate, FileAccess.Write);
31InBlock.gif        //fs.Write((Convert.FromBase64String(strData), 0,nSize);
32InBlock.gif        //fs.Close();
33ExpandedSubBlockEnd.gif    }

34ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值