?
Solved

c# replace file stream with string

Posted on 2010-08-12
3
Medium Priority
?
1,169 Views
Last Modified: 2013-12-17
I am trying to convert some code that writes to a file to return a string.

how do I get rid of the part where I write to a file and just return a string?

        public static void XMLSerializeClass(object classToSerialize, string fileNameToWriteTo)
        {
            // Initialize a storage medium to hold the serialized object
            Stream stream = new FileStream(fileNameToWriteTo, FileMode.Create, FileAccess.Write, FileShare.Write);

            // Serialize an object into the storage medium referenced by 'stream' object.
            XmlSerializer xmlserializer = new XmlSerializer(classToSerialize.GetType());
            xmlserializer.Serialize(stream, classToSerialize);

            // Cleanup
            stream.Close();
        }
0
Comment
Question by:jackjohnson44
[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
3 Comments
 
LVL 8

Accepted Solution

by:
mac-will earned 668 total points
ID: 33421131
public static void XMLSerializeClass(object classToSerialize, string fileNameToWriteTo)
        {
            // Initialize a storage medium to hold the serialized object
            Stream stream = new MemoryStream()

            // Serialize an object into the storage medium referenced by 'stream' object.
            XmlSerializer xmlserializer = new XmlSerializer(classToSerialize.GetType());
            xmlserializer.Serialize(stream, classToSerialize);


             stream .Position = 0;
      StreamReader sr = new StreamReader(m);
      string s = sr.ReadToEnd();

        }
0
 
LVL 10

Assisted Solution

by:Nash2334
Nash2334 earned 668 total points
ID: 33421151
You can serialize to a StringWriter instead of the Stream and then return the StringWriter instead.
0
 
LVL 7

Assisted Solution

by:jdavistx
jdavistx earned 664 total points
ID: 33421272
I was thinking the same as Nash.
public static string XMLSerializeClass(object classToSerialize)
{
	string serialzied;

	using( StringWriter sw = new StringWriter() )
	{
		XmlSerializer xmlserializer = new XmlSerializer(classToSerialize.GetType());
		xmlserializer.Serialize(sw, classToSerialize);
		serialzied = sw.ToString();
	}

	return serialzied;
}

Open in new window

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

741 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