当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > .Net中将图片数据保存到XML文档

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 .Net中将图片数据保存到XML文档


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 38 ::
收藏到网摘: n/a

  因为最近要做的项目中,我要通过XML动态生成窗体,看了UI图样,我有些叫苦:我通过XML动态生成窗体,可是主窗体中UI要用图标来确定要使用的窗体,怎么才能使主窗体的图标也是动态加载而且图标和要生成的窗体还有关联呢?我又想到用XML,查MSDN,看到只有XmlTextWriter和XmlTextReader里分别有XmlTextWriter.WriteBase64和XmlTextReader.ReadBase64可以操作图片这种二进制字节的数据。但是XmlTextWriter和XmlTextReader远不如XmlDocument操作方便,如果用这两者我就得写太多的代码。
困扰了我一天,记得以前看到过一篇文章介绍怎样将图片数据存储到Xml文件,可是怎么也找不到,后来终于在一个英文网站上找到了相关内容,而且还是2003年贴出来的,汗。
好了,不废话了,我把我的实现代码贴给大家吧。
private XmlDocument document;
private string FilePath = Application.StartupPath + "\\..\\..\\FormStyle.xml";   // FormStyle.xml 文件地址
 
private void frmMain_Load(object sender, System.EventArgs e)
{
       if(document == null)
       {
              document = new XmlDocument();
              document.Load(FilePath);
       }
 
       // 只挑选含有Form的节点
       XmlNodeList FormNodes = document.GetElementsByTagName("Form");
       lbIcons.BeginUpdate();
       lbIcons.Items.Clear();
       foreach(XmlNode node in FormNodes)
       {
              // 把节点的名称放到下拉列表里
              lbIcons.Items.Add(node.Attributes["Name"].Value);
       }
       lbIcons.EndUpdate();
}
 
private void lbIcons_SelectedValueChanged(object sender, System.EventArgs e)
{
       // 查找下拉框所选的窗体下是否有Image元素,若无则退出
       XmlNode node = document.DocumentElement.SelectSingleNode(string.Format("descendant::Form[@Name='{0}']/Image", lbIcons.SelectedItem.ToString()));
       if(node == null)
              return;
 
       // 如果含有Image元素,就将元素值转换为Base64String,然后放到内存流
       using (MemoryStream mem = new MemoryStream(Convert.FromBase64String(node.InnerText)))
       {
              // 加载内存流数据为位图
              Bitmap bmp = Bitmap.FromStream(mem) as Bitmap;
              pictureBox1.Image = bmp;
       }
}
 
private void btnAdd_Click(object sender, System.EventArgs e)
{
       // 如果不存在txtFilePath.Text所指文件,就退出
       if(!File.Exists(txtFilePath.Text) || lbIcons.Items.Count == 0)
              return;
 
       if(lbIcons.SelectedIndex == -1)
              lbIcons.SelectedIndex = 0;
 
       if(document == null)
       {
              document = new XmlDocument();