如何预先添加流?
我将 blob 作为字节数组从数据库中加载,并将它们放入内存流中,以便可以将它们加载到 xml 文档中进行解析。
然而,有些 blob 具有多个根节点,这会导致解析器崩溃。
我的解决方案是创建一个包含整个 blob 的新根节点。
我可以使用流编写器很好地添加到末尾,但是我不知道如何添加到开头。
如何添加到流之前?
更新
我在让它发挥作用时遇到了太多麻烦。我提取的“XML”不是正确的 XML,我必须不断添加越来越多的正则表达式才能在 XmlDocument 加载之前删除错误的 XML。我最终使用 HtmlAgilityPack 解析出 XML 的有效部分,并将它们放入自己的 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.
However there are blobs that have multiple root nodes, this causes the parser to blow up.
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?
Update
I was having too much trouble getting this to work. The "XML" I was extracting was not proper XML and I kept on having to add more and more regexes to remove bad XML before the XmlDocument Load. I ended up using the HtmlAgilityPack to parse out my valid sections of XML and I put those inside their own xml documents. Not the nicest solution but it works. Sigh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您已经从数据库中获得了 byte[] 数组,因此将数组前后的更多字节写入内存流应该很容易:
但是由于
XmlDocument
还提供了LoadXml( str)
,我觉得操作字符串应该是更直接的解决方案:Since you already have
byte[]
array from DB, writing more bytes before and after the array to memory stream should be easy:But since
XmlDocument
also provideLoadXml(str)
, I feel manipulating the string should be more straight forward solution:你不能直接。这导致两个选择:
You can't directly. This leads to two options:
这是我使用的:
This is the one I use:
一种简洁的方法是实现一个 CompositeStreamReader,它将接受多个流,然后按顺序读出它们。
https://web.archive.org/web/20100721082808/http://blogs.msdn.com/b/paolos/archive/2010/04/08/how-to-boost-message-transformations-using -the-xslcompiledtransform-class-extended.aspx 您可以适应,但您可以使用更简单的东西。
A clean way to do this is to implement a
CompositeStreamReader
that will accept a number of streams and then read them out in order.There is an implementation of one at https://web.archive.org/web/20100721082808/http://blogs.msdn.com/b/paolos/archive/2010/04/08/how-to-boost-message-transformations-using-the-xslcompiledtransform-class-extended.aspx that you can adapt, but you can get away with something simpler.