Solved

byte[] to file

Posted on 2011-09-26
2
237 Views
Last Modified: 2012-05-12
Hi all

Given the code below..
and given that <<Data>> is an XML file
How can I output/write the value to an xml file in a directory = @"C:\Inetpub\ftproot\Protocoll\Filename.xml";  

Thanks for your input
public class DownloadFile 
{ 
public byte[] Data; 
public String Filename; 
}

Open in new window

0
Comment
Question by:ZURINET
2 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36598029

DataTable dt = new DataTable();
dt.Columns.Add("dabyte", byte);

dt.Rows.Add(Data)
 

dt.WriteXML("C:\dabytes.xml");

refer
http://www.daniweb.com/software-development/csharp/threads/60139
0
 
LVL 22

Accepted Solution

by:
pivar earned 300 total points
ID: 36598076
Hi,

If I understand you correct you already have the xml-file in a byte array and want to write that to file. In that case, try
(from http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Save-byte-array-to-file.html)

/peter

 /// <summary>
/// Function to save byte array to a file
/// </summary>
/// <param name="_FileName">File name to save byte array</param>
/// <param name="_ByteArray">Byte array to save to external file</param>
/// <returns>Return true if byte array save successfully, if not return false</returns>
public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
    try
    {
        // Open file for reading
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        // Writes a block of bytes to this stream using data from a byte array.
        _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

        // close file stream
        _FileStream.Close();

        return true;
    }
    catch (Exception _Exception)
    {
        // Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }

    // error occured, return false
    return false;
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question