Base64 编码/解码 xml .net 中的大文件

发布于 2024-11-14 20:53:45 字数 1486 浏览 3 评论 0原文

我有一个外部网络服务,它返回字节数组中的文件(小,大20Mb文件),它必须转换为base64字符串并包含在XML文件中

编码

我用来转换字节数组的代码to base64

 Convert.ToBase64String(bytearray, 0, bytearray.Length, _
               Base64FormattingOptions.InsertLineBreaks)

下面是我使用 Linq to XML 的实际 xml 构造。

    attachment = From Item In cpd _
      Select New XElement("attachment", _
          New XAttribute("id", Item.UniqueID), _
       New XElement("attachmentDocumentInformation", _
       New XElement("actor", New XAttribute("reference", Item.AttchRefId)), _
       New XElement("documentDescription", _
       New XElement("documentTitle", Item.Document), _
       New XElement("documentType", "A"), _
       New XElement("sequence", Item.Sequence))), _
       New XElement("documentContent", _
         New XAttribute("contentEncoding", "base64"), _
         New XAttribute("id", "DocumentContent" + Item.UniqueID), _
         New XAttribute("mimeType", Item.mimeType), _ 
                 Convert.ToBase64String(Item.Content, 0, Item.Content.Length, _ 
                 Base64FormattingOptions.InsertLineBreaks)))

解码

,我使用 frombase64 转换接收到的 xml 文件,

 Convert.FromBase64String(documentContentinBase64)

当文件较小时,它工作正常,当文件较大时,转换返回“不支持此类接口”。

我有两个问题:

  1. 将大文件转换为base64/frombase64 的最佳方法是什么?
  2. 将字节数组转换为 Base64 后,构造包含 Base64 文档的 xml 文件的最佳方法是什么。有人有代码吗?

谢谢

i have a external webservice which returns the files in byte array (small, large 20Mb file's), it has to be converted to base64 string and include in XML file

Encode

the code i used to convert the byte array to base64 is

 Convert.ToBase64String(bytearray, 0, bytearray.Length, _
               Base64FormattingOptions.InsertLineBreaks)

the below is the actual xml construction i use Linq to XML.

    attachment = From Item In cpd _
      Select New XElement("attachment", _
          New XAttribute("id", Item.UniqueID), _
       New XElement("attachmentDocumentInformation", _
       New XElement("actor", New XAttribute("reference", Item.AttchRefId)), _
       New XElement("documentDescription", _
       New XElement("documentTitle", Item.Document), _
       New XElement("documentType", "A"), _
       New XElement("sequence", Item.Sequence))), _
       New XElement("documentContent", _
         New XAttribute("contentEncoding", "base64"), _
         New XAttribute("id", "DocumentContent" + Item.UniqueID), _
         New XAttribute("mimeType", Item.mimeType), _ 
                 Convert.ToBase64String(Item.Content, 0, Item.Content.Length, _ 
                 Base64FormattingOptions.InsertLineBreaks)))

Decode

and i use frombase64 conversion for the received xml file

 Convert.FromBase64String(documentContentinBase64)

it works fine when the file's are smaller size, when it's a large file conversion returns "No such interface supported".

I have two questions:

  1. what is the best way to convert large files tobase64/frombase64 ?
  2. after converting the byte array to base64 what is the best way to construct xml file with base64 documents in it. Does anyone have some code?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2024-11-21 20:53:45

这大致遵循您的文档,简单地按摩一些名称,或者添加 xml 序列化器属性来获取您想要的 xml 文档:

public class Document
{
    public string Actor { get; set; }
    public string Description { get; set; }
    public string Title { get; set; }
    public string type { get { return "A"; } }
    public int Sequence { get; set; }
    public byte[] Content { get; set; }
}


var d = new Document() { Actor = "Sean Connory", Description = "Thriller", Title = "The Rock" };
d.Content = new byte[] { 43,45,23,43,82,90,34 };

var xmls = new System.Xml.Serialization.XmlSerializer(typeof(Document));
using (var ms = new System.IO.MemoryStream())
{
    xmls.Serialize(ms, d);
    Console.Write(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
}
Console.ReadLine();

XmlSerializer 将转换 byte[] 属性(在此case Content) 自动与 Base64 编码进行转换。您正在寻找转换大文件并将其放入 xml 文档的“最佳”方法。他们可能是其他(更好的)方法。但我过去曾使用过这种方法并取得了巨大的成功。如果设置正确,此解决方案可以为您节省很多麻烦,因为如果对象设置正确,它会为您构建 xml 文档并将数据转换为 Base64。另一方面,您可以获取 xml 文档并使用其所有数据填充对象,这样可以节省导航 xml 节点以查找所需数据的时间。

更新

如果这对大文件不起作用,我确实找到了这篇关于将流序列化为 Base64 流的 MSDN 文章。我以前从未处理过这个问题,因此无法为您提供任何深刻的见解,但这听起来更像是您正在寻找的东西。

This roughly follows your document, simple massage some names, or add xml serializer attributes to get the xml document you want:

public class Document
{
    public string Actor { get; set; }
    public string Description { get; set; }
    public string Title { get; set; }
    public string type { get { return "A"; } }
    public int Sequence { get; set; }
    public byte[] Content { get; set; }
}


var d = new Document() { Actor = "Sean Connory", Description = "Thriller", Title = "The Rock" };
d.Content = new byte[] { 43,45,23,43,82,90,34 };

var xmls = new System.Xml.Serialization.XmlSerializer(typeof(Document));
using (var ms = new System.IO.MemoryStream())
{
    xmls.Serialize(ms, d);
    Console.Write(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
}
Console.ReadLine();

the XmlSerializer will convert the byte[] property (in this case Content) automatically to and from base64encoding. You were looking for the 'best' way to convert large files and to put them in xml documents. Their my be other (better) ways of doing so. But I have used this way with a great amount of success in the past. If setup correctly this solution could save you a lot of trouble as it will both build the xml document for you and convert the data to Base64 if the object is setup correctly. On the reverse side you can take an xml document and populate an object with all of its data which saves you the time of navigating xml nodes to find the data you want.

Update

If this doesn't work for large files as expected, I did find this MSDN article on serializing a stream into a base64 stream. I have never worked with this before and so cannot provide any sort of great insight for you but it sounds more like something you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文