Solved

byte[] to file

Posted on 2011-09-26
2
228 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now