Solved

c# replace file stream with string

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

789 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