Solved

How do I convert a byte array into XML file?

Posted on 2007-11-14
10
6,791 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 23

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
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.

738 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