Solved

How do I convert a byte array into XML file?

Posted on 2007-11-14
10
6,764 Views
Last Modified: 2012-06-21
I have a column of data in SQL that is stored as an image/binary data type but it is an XML file.  I can retrieve from SQL and create a byte array.  How do I take that byte array and convert back into an XML file
0
Comment
Question by:Jtriolo
10 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 20283256
0
 
LVL 3

Expert Comment

by:Aurora27
ID: 20283372
Using the MemeoryStream object is the best way to do it - Never convert it into a string and then write it - it will truncate it.
0
 

Author Comment

by:Jtriolo
ID: 20283442
OK, I'm using ADO and bringing in the data into a dataset and then taking the column and converting into a byte array as follows:

byte[] ba = (byte[])ds.Tables["Images"].Rows[0]["ImgData"];
 MemoryStream ms = new MemoryStream(ba, true);
                ms.Write(ba, 0, ba.Length);

I'm lost after this.  I have other columns that I am having no problem converting into .jpg....
What code do I use to write the Memory stream into an XML file on the hard drive?

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Expert Comment

by:James Murrell
ID: 20283497
0
 

Author Comment

by:Jtriolo
ID: 20283545
should I not be using a memory stream?
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 20283681
I am unsue sorry
0
 

Author Comment

by:Jtriolo
ID: 20283924
OK I figured it out.......  As I said before,  used ADO and brought the data into a dataset and then took the column and converted into a byte array as follows:

 byte[] ba = (byte[])ds.Tables["Images"].Rows[0]["OldXML_Data"];

            if (ba.Length > 0)
            {
                MemoryStream ms = new MemoryStream(ba, true);
                ms.Write(ba, 0, ba.Length);
                FileStream fs = new FileStream(@"C:\MyXML.xml", FileMode.Create);
                fs.Write(ba, 0, ba.Length);
                ms.Close();
            }

0
 
LVL 31

Expert Comment

by:James Murrell
ID: 20283969
great well done... shame i could not offer more
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 125 total points
ID: 20285247
Code is fine, just one thing.  If you actually want to write the file to disk, then there's no need to have the memory stream in there, you're just writing the byte[] data into 2 different streams, the ms and the fs (you also need to close the FileStream).

byte[] ba = (byte[])ds.Tables["Images"].Rows[0]["OldXML_Data"];

            if (ba.Length > 0)
            {
                FileStream fs = new FileStream(@"C:\MyXML.xml", FileMode.Create);
                fs.Write(ba, 0, ba.Length);
                fs.Close();
            }


The only reason to use the memory stream, is if you just want to load the data into an XmlDocument variable or something like that, and not actually write it to disk.
0
 

Author Comment

by:Jtriolo
ID: 20292668
I know I realized after that I didn't need the 2 lines of code for the memory stream, etc....   Thanks all!!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

786 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