Solved

c# replace file stream with string

Posted on 2010-08-12
3
1,111 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 167 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 167 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 166 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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