gds通用软件开发系统

导航

Base64--在XML中存储图片的解决方案

其实,在他们的XML中,图片是从 二进制文件  转换为 Base64编码 存储在文件中的. 这样做的好处,就是可以缩小XML的体积.
而从XML中读出 base64 编码后, 需要解码成 二进制 ,再写入图片.
如果采用C#来处理这个过程,可以利用.net自带的类来处理.
解码:  data = System.Convert.FromBase64String(labelContent);
我的原始方法是这样的:

public virtual string PrintLable(string replyXml)
{
string result = "";

            XmlDocument replyDoc = new XmlDocument();
            replyDoc.LoadXml(replyXml);
//XmlNode errorNode = replyDoc.SelectSingleNode(errorPath);
if (this.IsSendSucessfully(replyDoc))
{
//no error
                XmlNode labelNode = replyDoc.SelectSingleNode(labelContentPath);



string labelContent = labelNode.InnerText;

                replyDoc = null;
                labelNode = null;




byte[] data = new byte[labelContent.Length];



                data = System.Convert.FromBase64String(labelContent);


string fileName = box.WORK_ORDER_ID.ToString() + box.BOX_ID.ToString() + exeName;

                FileStream fs = null;
try
{
                    fs = File.OpenWrite(LABELFOLDER + fileName);
                    fs.Write(data, 0, data.Length);
                    result = "Please Check Labels in " + LABELFOLDER + ",Labels are there!";

                }
catch (Exception e)
{
                    result = e.Message;
                }
finally
{
                    fs.Flush();
                    fs.Close();

                    IsPrintSuccess = true;
                }
            }
else
{
//XmlNode errorMsgNode = replyDoc.SelectSingleNode();
//result = errorMsgNode.InnerText;
                result = replyDoc.OuterXml;

            }

return result;



        }

posted on 2010-03-24 14:08  gds通用软件开发系统  阅读(680)  评论(0编辑  收藏  举报