文件到XML存储..请帮助! [英] File to XML storage.. Please Help!

查看:161
本文介绍了文件到XML存储..请帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件(二进制数据)写入XML文件中.代码如下所示:

I am trying to write a File (Binary Data) into an XML file... The code can be seen below:

private void FillFileData(string file)
{
                DataSet xmlFile = new DataSet("File");
    DataTable table;
    table = xmlFile.Tables.Add("Data");
    table.Columns.Add("Name",typeof(string));
    table.Columns.Add("MD5",typeof(string));
    table.Columns.Add("data",typeof(string));
    StreamReader sr = new StreamReader(file);
    table.Rows.Add(Path.GetFileName(file),"x",sr.ReadToEnd());
    sr.Close();
                xmlFile.WriteXML("test.xml");
                xmlFile.Dispose();
}


我真的很感谢任何评论,因为我真的不知道应该在哪种类型中添加数据"列以及应该使用什么类型将数据读入其中...

如果有人可以提供有关获取MD5的信息,那也很棒.


I would really appreciate any comments since i am really out of ideas here on what type should i add the Column "data" and what should i use to read the data into there...

If by any chance someone can provide info on getting the MD5 as well that would be great.

推荐答案

如果您要存储二进制数据,则需要更改列的类型.

您的md5签名也应更改.

您缺少架构,因此必须在加载数据之前定义架构.

因此,我修复了所有问题,并为您重新编写了函数,包括生成哈希.

well if you want to store binary data, you need to change the type of your column.

your md5 signature should also be changed.

you are missing your schema, so you would have to define your schema before loading your data.

So I fixed all that and rewrote your function for you, including generating the hash.

private string FillFileData(string file)
{
    using (var xmlFile = new DataSet("File"))
    {
        using (var table = xmlFile.Tables.Add("Data"))
        {
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("MD5", typeof(byte[]));
            table.Columns.Add("data", typeof(byte[]));
            byte[] b = File.ReadAllBytes(file);
            var hasher = System.Security.Cryptography.MD5.Create();
            var outputHash = hasher.ComputeHash(b);
            var fileName = Path.GetFileName(file);
            var oo = new object[] { fileName, outputHash, b };
            table.Rows.Add(oo);
            string outputFileName = Path.GetTempFileName();
            using (var sw = new StreamWriter(outputFileName))
            {
                xmlFile.WriteXmlSchema(sw);
                xmlFile.WriteXml(sw);
            }
            return outputFileName;
        }
    }
}


这篇关于文件到XML存储..请帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆