Solved

How do I convert a byte array into XML file?

Posted on 2007-11-14
10
6,752 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
 
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

912 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

21 Experts available now in Live!

Get 1:1 Help Now