如何在视频流之前添加视频? [英] How do I prepend a Stream?

查看:36
本文介绍了如何在视频流之前添加视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将blob作为字节数组从数据库中加载出来,并将它们放入内存流中,以便可以将它们加载到xml文档中进行解析.

I'm loading blobs out of a database as a byte array and I put them in a memory stream so that I can load them into an xmldocument for parsing.

但是,有多个具有多个根节点的blob,这会导致解析器崩溃.

However there are blobs that have multiple root nodes, this causes the parser to blow up.

我的解决方案是只制作一个包含整个blob的新根节点.

My solution is to just make a new root node that encompasses the whole blob.

我可以用流式写入器添加到末尾,但是我不知道如何添加到末尾.

I can add onto the end just fine with a streamwriter however I can't figure out how to add onto the beginning.

我怎样才能添加到视频流?

How can I prepend to a stream?

更新
我在使它工作时遇到了很多麻烦.我提取的"XML"不是正确的XML,在XmlDocument加载之前,我一直不得不添加越来越多的正则表达式来删除错误的XML.我最终使用HtmlAgilityPack解析了XML的有效部分,并将它们放入自己的xml文档中.不是最好的解决方案,但是它可以工作.叹气

推荐答案

由于您已经从DB获得了 byte [] 数组,因此在数组之前和之后向内存流中写入更多字节应该很容易:

Since you already have byte[] array from DB, writing more bytes before and after the array to memory stream should be easy:

// bytes from db
byte[] multipleNodes = Encoding.UTF8.GetBytes("<first>..</first><second>..</second><third>..</third>");

using (var ms = new MemoryStream())
{
    // write opening tag
    byte[] newRoot = Encoding.UTF8.GetBytes("<newRoot>");
    ms.Write(newRoot, 0, newRoot.Length);

    ms.Write(multipleNodes, 0, multipleNodes.Length);

    // write opening tag
    byte[] closeNewRoot = Encoding.UTF8.GetBytes("</newRoot>");
    ms.Write(closeNewRoot, 0, closeNewRoot.Length);

    // reset cursor position before pass it to xmldoc
    ms.Position = 0;

    var xml = new XmlDocument();
    xml.Load(ms);

    Console.WriteLine(xml.InnerXml);
}

但是由于 XmlDocument 还提供了 LoadXml(str),所以我认为操纵字符串应该是更直接的解决方案:

But since XmlDocument also provide LoadXml(str), I feel manipulating the string should be more straight forward solution:

// bytes from db
byte[] multipleNodes = Encoding.UTF8.GetBytes("<first>..</first><second>..</second><third>..</third>");

string stringFromBlob = Encoding.UTF8.GetString(multipleNodes);
string withRootNode = string.Format("<newRoot>{0}</newRoot>", stringFromBlob);

var xml = new XmlDocument();
xml.LoadXml(withRootNode);

Console.WriteLine(xml.InnerXml);

这篇关于如何在视频流之前添加视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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