2011-06-09 146 views
1

我有它返回字节数组的文件外部web服务(小,大20MB的文件的),它必须被转换成BASE64的字符串和包括在XML文件的Base64编码/解码的大文件

编码

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

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

下面是实际的XML结构我使用LINQ到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. 什么是大文件转换tobase64/frombase64的最佳方式?
  2. 在将字节数组转换为base64之后,构建带有base64文档的xml文件的最佳方式是什么?有没有人有一些代码?

感谢

+0

'Convert.FromBase64String'没有这样的签名。你能确认你的代码吗? – Jay 2011-06-09 16:31:13

+1

当你说“爆炸”时,你的意思是什么;有没有例外?如果是这样,请发布例外的内容。 – Jacob 2011-06-09 16:32:10

+0

另外假设在这一点上,bContent是'documentContent'节点的值吗?为什么不序列化一个对象并让'XmlSerializer'为你做所有的工作? – Jay 2011-06-09 16:34:31

回答

1

这大致如下文档,简单的按摩一些名字,或添加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(); 

XmlSerializerbyte[]属性转换(在这种情况下内容)自动进出base64encoding。您正在寻找将大文件转换为xml文件的“最佳”方法。他们是我的其他(更好的)方式。但我过去曾以这种方式取得了巨大的成功。如果安装正确,该解决方案可以为您节省很多麻烦,因为它将为您构建xml文档,并在数据正确设置时将数据转换为Base64。反过来,你可以获取一个XML文档,并使用它的所有数据填充一个对象,这样可以节省导航xml节点的时间以查找所需的数据。

更新

如果这不适合大文件按预期方式工作,我做了序列化流为Base64流找到this MSDN article。我以前从未与之合作过,因此无法为你提供任何有用的信息,但这听起来更像你正在寻找的东西。

+1

只是为了检查这个建议不是关于如何使用Base64,而是序列化文件的另一种方式,应该更强大?你可能想提供一些更多的解释,因为我的第一个想法是“downvote,因为它似乎没有与base64编码有关”。 – Chris 2011-06-09 16:58:28

+0

是的,我的观点更多的是你可能想解释这就是你在回答中所做的。就其本身而言,你所暗示的究竟是什么并不明显。 – Chris 2011-06-10 08:31:54

+0

使用XMLSerializer我能够成功处理小文件的大文件,但我不成功。我试过'Debug.Write(System.Text.Encoding.UTF8.GetString(ms.ToArray()))'并且在它正在构建base64string的调试输出窗口看到它停止了,我没有得到任何错误并且输出窗口不显示内容元素,但具有其他元素和值。我在这里错过了什么? – zeusmos 2011-06-10 21:39:21