ghx88

XML中的二进制文件的编码与解码[原创]

(一)把二进制文件放到XML中
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class OutPutXML : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
int readByte = 0;
        
int filesize = 0;
        FileStream fs 
= new FileStream("C:\\060407color01.jpg", FileMode.Open);
        filesize 
= Convert.ToInt32(fs.Length);
        
string filepath = Server.MapPath(Request.ApplicationPath) + "\\" + System.Configuration.ConfigurationManager.AppSettings["UploadDir"].ToString() + "\\" + "test.xml";
        
int index = fs.Name.LastIndexOf("\\"+ 1;
        
string filename = fs.Name.Substring(index, (fs.Name.Length - index));

        BinaryReader br 
= new BinaryReader(fs);
        XmlTextWriter wt 
= new XmlTextWriter(filepath, Encoding.UTF8);

        wt.WriteStartDocument();
        wt.WriteStartElement(
"Upload");
        wt.WriteStartElement(
"username""guest");
        wt.WriteEndElement();
        wt.WriteStartElement(
"password""123456");
        wt.WriteEndElement();
        wt.WriteStartElement(
"file");
        wt.WriteAttributeString(
"size", filesize.ToString()); //文件的大小
        wt.WriteAttributeString("name", filename);//文件名

        
//以base64编码文件,并添加到XML中的元素中
        byte[] base64buffer = new byte[filesize];
        
do
        
{
            readByte 
= br.Read(base64buffer, 0, filesize);
            wt.WriteBase64(base64buffer, 
0, readByte);
        }
 while (filesize <= readByte);

        wt.WriteEndElement();
        wt.WriteEndElement();
        wt.WriteEndDocument();
        wt.Close();
        fs.Close();
        br.Close();
    }

}


(二)从XML中读出数据、解码、保存
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Xml;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerEasy.BLL;
using Power.Model;
using Power.Rss;

public partial class Rss_Upload : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
int readByte = 0;
        
int filesize = 0;
        
string username = string.Empty;
        
string password = string.Empty;
        
string uploadPath = Server.MapPath(Request.ApplicationPath) + "\\"+ System.Configuration.ConfigurationManager.AppSettings["UploadDir"].ToString()+"\\";
        
string filename = string.Empty;
        
string newFileName = string.Empty;
        
string exName = string.Empty; //扩展名
        ProblemXML xml = new ProblemXML();
        BinaryWriter bw 
= null;
        FileStream fs 
= null;
        XmlTextReader xmlrd 
= null;

        xmlrd 
= new XmlTextReader(uploadPath + "test.xml");//Request.InputStream
        
        
try
        
{
            
while (xmlrd.Read())
            
{
                
if (xmlrd.NodeType == XmlNodeType.Element && xmlrd.Name == "username"
                
{
                    username 
= xmlrd.GetAttribute(0).Trim();
                }

                
if (xmlrd.NodeType == XmlNodeType.Element && xmlrd.Name == "password")
                
{
                    password 
= xmlrd.GetAttribute(0).Trim();
                    
if (!xml.CheckUser(username, password))
                    
{
                        xml.PutInfo(ProblemXML.UPLOAD_ERR, ProblemXML.INPUT_PASSWORD_ERROR);
                        
return;
                    }

                }

               
                
if (xmlrd.NodeType == XmlNodeType.Element && xmlrd.Name.Trim() == "file")
                
{
                    
try
                    
{
                        filesize 
= Convert.ToInt32(xmlrd.GetAttribute("size"));
                        filename 
= xmlrd.GetAttribute("name");
                        exName 
= CommonFunction.FileValidator(filename);
                        
if (exName == string.Empty) 
                        
{
                            xml.PutInfo(ProblemXML.UPLOAD_ERR,
"上传的文件只可以是 JPG 、GIF 、BMP 、PNG 、ZIP 、RAR 类型! ");
                            
return;
                        }

                        newFileName 
= DateTime.Now.Ticks.ToString() + "."+exName;
                        fs 
= new FileStream(uploadPath + newFileName , FileMode.Create);
                        bw 
= new BinaryWriter(fs);
                        
byte[] base64buffer = new byte[filesize];
                        
do
                        
{
                            readByte 
= xmlrd.ReadBase64(base64buffer, 0, filesize);
                            bw.Write(base64buffer, 
0, readByte);

                        }
 while (readByte >= filesize);
                        bw.Close();
                        fs.Close();
                    }

                    
catch (Exception ex1)
                    
{
                        xml.PutInfo(ProblemXML.UPLOAD_ERR,ex1.Message);
                        
return;
                    }

                }
//end of if
            }
//end of while
           
        }

        
catch 
        
{
            
//hrow new Exception(ex2.Message);
            xml.PutInfo(ProblemXML.UPLOAD_ERR, ProblemXML.INPUT_ERROR);
            
return;
        }

        
finally
        
{
            xmlrd.Close();
        }


        
if (File.Exists(uploadPath + newFileName))
        
{
            xml.PutInfo(ProblemXML.UPLOAD_OK, newFileName);
        }

        
else 
        
{
            xml.PutInfo(ProblemXML.UPLOAD_ERR, ProblemXML.UPLOAD_ERR_INFO);
        }

      
    }


}


posted on 2006-07-03 17:32  ghx88  阅读(581)  评论(0编辑  收藏  举报

导航