Solved

How do I convert a byte array into XML file?

Posted on 2007-11-14
10
6,772 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

789 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