开发者

Base64 encode/decode large files in xml .net

开发者 https://www.devze.com 2023-03-11 23:17 出处:网络
i have a external webservice which returns the files in byte array (small, large 20Mbfile\'s), it has to be converted to base64 string and include in XML file

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 开发者_如何学Cactual 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消