Solved

c# replace file stream with string

Posted on 2010-08-12
3
1,040 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 to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

11 Experts available now in Live!

Get 1:1 Help Now