从XmlReader中,德的base64 code将其流读取和写入结果文件 [英] Read stream from XmlReader, base64 decode it and write result to file

查看:417
本文介绍了从XmlReader中,德的base64 code将其流读取和写入结果文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想从的XmlReader 和直接的base64德code为流解压缩到一个文件中。

Basically, I want to extract the stream from the XmlReader and directly base64 decode it to a file.

XML文件的结构可以看出<一href=\"http://stackoverflow.com/questions/30095256/xmlreader-get-value-of-child-node-if-another-child-node-contains-a-special-stri\">here.为了让我不得不使用值 ReadInnerXml()。是否有可能使用 ReadValueChunk 呢?

The structure of the XML file can be seen here. To get the value I have to use ReadInnerXml(). Is it possible to use ReadValueChunk instead?

下面是我目前的code:

Here is my current code:

using (XmlReader reader = XmlReader.Create("/your/path/47311.xml"))
{
    while(reader.Read())
    {
        if (reader.IsStartElement () && reader.NodeType == XmlNodeType.Element) {
            switch (reader.Name) {
            case "ttOutputRow":
                reader.ReadToDescendant ("cKey");
                switch (reader.ReadInnerXml ()) {
                case "findMe":
                    reader.ReadToNextSibling ("cValue");
                    // here begins the interesting part
                    char[] buffer = new char[4096];
                    int charRead;
                    using (var destStream = File.OpenWrite ("/your/path/47311.jpg")) {
                        while ((charRead = reader.ReadValueChunk (buffer, 0, 4096)) != 0) {
                            byte[] decodedStream = System.Convert.FromBase64String (new string (buffer));
                            await destStream.WriteAsync(decodedStream, 0, decodedStream.Length);
                            Console.WriteLine ("in");
                        }

                    }
                    break;
                default:
                    break;
                }
                break;
            default:
                break;
            }
        }
    }
}

目前,他不读的价值。

我不能使用 ReadValueChunk 这个?我怎么能直接从的XmlReader 使用流,而不牺牲太多的内存?

Can't I use ReadValueChunk for this? How can I directly use the stream from the XmlReader without sacrificing too much memory?

编辑:

根据 DBC 我修改了code。这是我目前使用的:

According to dbc I modified my code. This is what I currently use:

using (XmlReader reader = XmlReader.Create("test.xml"))
{
    while(reader.Read())
    {
        if (reader.IsStartElement () && reader.NodeType == XmlNodeType.Element) {
            switch (reader.Name) {
            case "ttOutputRow":
                reader.ReadToDescendant ("cKey");
                switch (reader.ReadInnerXml ()) {
                case "findMe":
                    reader.ReadToNextSibling ("cValue");
                    byte[] buffer = new byte[40960];
                    int readBytes = 0;
                    using (FileStream outputFile = File.OpenWrite ("test.jpg")) 
                    using (BinaryWriter bw = new BinaryWriter(outputFile))
                    {
                        while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, 40960)) > 0) {
                            bw.Write (buffer, 0, readBytes);
                            Console.WriteLine ("in");
                        }

                    }
                    break;
                default:
                    break;
                }
                break;
            default:
                break;
            }
        }
    }
}

这里你可以找到一个测试文件。真正的文件是有点大了,因此需要更多的时间。

Here you can find a test file. The real file is a little bit bigger and therefore takes much more time.

如预期上述code不起作用。这是非常慢和所提取的图像主要是黑色(破坏)。

The above code doesn't work as expected. It is very slow and the extracted image is mostly black (destroyed).

推荐答案

为了给出一个明确的回答你的问题,我需要看到你正在试图读取XML。但是,有两点:

In order to give a definitive answer to your question I would need to see the XML you are trying to read. However, two points:


  1. 据该的 =https://msdn.microsoft.com/en-us/library/system.convert.frombase64string%28v=vs.110%29.aspx相对=nofollow > Convert.FromBase64String

  1. According to the documentation for Convert.FromBase64String:

该FromBase64String方法中将被设计来处理包含所有数据是德codeD一个字符串。从流取消code基-64字符数据,使用 System.Security.Cryptography.FromBase64Transform 类。

The FromBase64String method is designed to process a single string that contains all the data to be decoded. To decode base-64 character data from a stream, use the System.Security.Cryptography.FromBase64Transform class.

因此​​,您的问题可能与块而不是块中阅读它的内容进行解码。

Thus your problem may be with decoding the content in chunks rather than with reading it in chunks.

您可以使用<一个href=\"https://msdn.microsoft.com/en-us/library/system.xml.xmlreader.readelementcontentasbase64%28v=vs.110%29.aspx\"相对=nofollow> XmlReader.ReadElementContentAsBase64 或<一个href=\"https://msdn.microsoft.com/en-us/library/system.xml.xmlreader.readelementcontentasbase64async%28v=vs.110%29.aspx\"相对=nofollow> XmlReader.ReadElementContentAsBase64Async 出于这样的目的。从文档:

You can use XmlReader.ReadElementContentAsBase64 or XmlReader.ReadElementContentAsBase64Async for exactly this purpose. From the docs:

此方法使用Base64编码读取元素含量,德codeS,然后返回去codeD二进制字节(例如,内嵌的Base64恩codeD GIF图像)到缓冲区。

This method reads the element content, decodes it using Base64 encoding, and returns the decoded binary bytes (for example, an inline Base64-encoded GIF image) into the buffer.

事实上,在文档中的例子演示了如何从一个XML文件中提取的base64恩codeD映像,并将其写入到块的二进制文件。

In fact, the example in the documentation demonstrates how to extract a base64-encoded image from an XML file and write it to a binary file in chunks.

这篇关于从XmlReader中,德的base64 code将其流读取和写入结果文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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