---
title: "assign textwriter to memory writer"  
description: "assign textwriter to memory writer"  
author: "Anonymous User"  
published: 2014-09-02  
updated: 2014-09-02  
canonical: https://www.mindstick.com/forum/2227/assign-textwriter-to-memory-writer  
category: "asp.net"  
tags: ["how to assign textwriter to memory writer", "textwriter to memory writer in c#", "mindstick", "what is textwrite and memory writer"]  
reading_time: 1 minute  

---

# assign textwriter to memory writer

I am [writing xml](https://www.mindstick.com/forum/33719/writing-xml-string-with-stax) file but [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) some value for specific field. i check that when object comes which contains value that specific value exist but after writing xml value doesn't exist. following is the code which is using. I think XmlTextWriter could be the cause of wrong xml. There is another [method](https://www.mindstick.com/forum/166/webservice-method) which could be used for it that is TextWriter but failed to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) it into [memorystream](https://www.mindstick.com/forum/161585/what-is-the-difference-between-filestream-and-memorystream).

[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) xmlString = null;

MemoryStream memoryStream = new MemoryStream();

XmlSerializer xs = new XmlSerializer(typeof(T));

// XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);

TextWriter xmlTextWriter=new [StreamWriter](https://www.mindstick.com/interview/34097/how-can-you-write-to-a-file-using-streamwriter-with-async-await)(memoryStream,Encoding.ASCII);

xs.Serialize(xmlTextWriter, obj);

memoryStream =(MemoryStream)xmlTextWriter. //(MemoryStream)xmlTextWriter.BaseStream;

xmlString = ASCIIByteArrayToString(memoryStream.ToArray()); return `xmlString;`

Any idea how i can know why and where the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) occur.

## Replies

### Reply by Sumit Kesarwani

Hi Tanuj, \

Try disposing properly your IDisposable resources by wrapping them in using statements:

public string SerializeToXml<T>(T obj)

{

using (var stream = new MemoryStream())

{

var xs = new XmlSerializer(typeof(T));

xs.Serialize(stream, obj);

return Encoding.UTF8.GetString(stream.ToArray());

}

}


---

Original Source: https://www.mindstick.com/forum/2227/assign-textwriter-to-memory-writer

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
