Solved

c# replace file stream with string

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

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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