【问题标题】:Conversion from Binary Data to XML using VB.net使用 VB.net 将二进制数据转换为 XML
【发布时间】:2014-12-05 18:01:20
【问题描述】:

我正在尝试从 SQL 中检索 VARBINARY 列,然后将二进制数据转换为 XML。我可以轻松检索数据并将其转换为 XML,但最近它开始截断转换后的 XML 文件的最后几个字符。下面是代码和说明。

这就是我检索二进制数据的方式。

Dim binaryData As Byte()
sqlData.Open()
        binaryCMD.CommandText = "select  photo from dbo.testing_filestream where clientName = '" &   clientSelected1 & "' and date = '" & minimumDate & "'"
        binaryCMD.CommandType = CommandType.Text
        binaryCMD.Connection = sqlData
        binaryData = binaryCMD.ExecuteScalar

然后,我将其转换为 MemoryStream。

Dim xmlStream As New IO.MemoryStream(binaryData)
            xmlStream.Seek(0, 0)

然后,我解压缩它(因为它处于压缩模式)。

Dim out_fs = New FileStream(XMLdataReader, FileMode.OpenOrCreate, FileAccess.Write)
            Dim unZip As DeflateStream = New DeflateStream(xmlStream, CompressionMode.Decompress)
            unZip.CopyTo(out_fs)
            unZip.Close()
            out_fs.Close()

但是,它会截断 xml 文件中的最后几个字符。 (XMLdataReader 只是一个包含文件名的字符串)。下面是截断的结果。我希望它完整,因为我想将该 XML 转换为数据表。

</DocumentEle
Whereas, it should give this in the format of
</DocumentElement>

你们能帮忙吗?我在谷歌上搜索了很多,直到我确定我只是在兜圈子,我才提出问题。除此之外,无论如何我可以将这个解压缩的二进制数据转换为 XML,而无需将其保存在文件中,然后将其转换为数据表。 多谢你们。 :)

【问题讨论】:

  • 使用 SQL 参数并且不要使用字符串作为日期值
  • 好的,谢谢。我会改变它,但它可以很好地检索数据。我已将二进制数据导出到 .cmp 文件,并将其与原始文件进行比较,结果是相同的。它只是截断 XML 的最后几个单词。你能帮我解决这个问题吗?

标签: xml vb.net binary compression deflatestream


【解决方案1】:

始终使用使用 IDisposable 资源来避免此类问题:

Using in_fs = File.OpenRead("abc.xml")
Using out_fs = File.Create("def.cmp")
    Using df_fs = New DeflateStream(out_fs, CompressionMode.Compress)
        in_fs.CopyTo(df_fs)
    End Using
End Using
End Using

Using in_fs = File.OpenRead("def.cmp")
Using out_fs = File.Create("abc2.xml")
    Using df_fs = New DeflateStream(in_fs, CompressionMode.Decompress)
        df_fs.CopyTo(out_fs)
    End Using
End Using
End Using

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    相关资源
    最近更新 更多