Solved

How do I convert a byte array into XML file?

Posted on 2007-11-14
10
6,737 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

705 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

20 Experts available now in Live!

Get 1:1 Help Now