Creating an XML file in temporary memory

  • Thread starter Thread starter Bharathi Harshavardhan
  • Start date Start date
B

Bharathi Harshavardhan

Hi,

I have a requirement where I need to create an xml file in temporary
memory and not on hard disk.

currently this is how I am creating an xml file using XmlTextWriter :
XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml",
null);

I do not want my xml file to be stored in C:/, I want it to be in
temporary memory,

Could anyone please assist me?
 
Must it be a file? Perhaps use a MemoryStream - attach the writer to
this stream using the ctor. Later, simpy rewind the stream and Read()
it. Alternatively, if you just want the xml, consider using a
StringBuilder as the target - then just call ToString().

Marc
 
Hi

In temporory memory there is nothing so called "FILE". Temporory
memory is just a logical address space. As suggested by Marc, I think
using Memory strams will do in this case.
Thanks
-Cnu
 
I have a requirement where I need to create an xml file in temporary
memory and not on hard disk.

currently this is how I am creating an xml file using XmlTextWriter :
XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml",
null);

I do not want my xml file to be stored in C:/, I want it to be in
temporary memory,

Could anyone please assist me?

Others have suggested MemoryStream, and that's a fine idea if you want
the binary data. If you want the data as text, a StringWriter is
probably slightly easier.

Jon
 
Must it be a file? Perhaps use a MemoryStream - attach the writer to
this stream using the ctor. Later, simpy rewind the stream and Read()
it. Alternatively, if you just want the xml, consider using a
StringBuilder as the target - then just call ToString().

In addition to Marc's fine suggestion, you might also consider a
StringWriter, which wraps up a StringBuilder in a TextWriter, which you
can then use to construct your XmlTextWriter instance. Maybe this is what
he means by "consider using a StringBuilder as the target", since I don't
see any direct way to actually do that (that is, I don't see anything in
XmlTextWriter that takes a StringBuilder as a direct target).

The main advantage to using the StringWriter is that you have easier
access to the resulting XML text. If that's not of concern to you, then
the MemoryStream should work just fine.

Pete
 
Others have suggested MemoryStream, and that's a fine idea if you want
the binary data. If you want the data as text, a StringWriter is
probably slightly easier.

Okay, I'll give you that one. You beat me by 7 seconds.

But only because I had to go back and check to see if I could figure out
what Marc meant by "using a StringBuilder as the target". :P
 
By which I meant:

StringBuilder sb = new StringBuilder();
using(XmlWriter writer = XmlWriter.Create(sb)) {
// write some xml
writer.Close();
}
string xml = sb.ToString();

Marc
 
Hi all,

Thank you all for the suggestions provided.

My problem got resolved :-)

I modified my code as below:

private string CreateXML()
{

StringBuilder sb = new StringBuilder();
XmlWriter xmlWriter = XmlWriter.Create(sb);
// Write xml
xmlWriter.Close();
return sb.ToString();
}

Thank you,
Regards,
Bharathi.
 
By which I meant:

StringBuilder sb = new StringBuilder();
using(XmlWriter writer = XmlWriter.Create(sb)) [...]

I see. I'll point out that the OP wasn't using an XmlWriter, so IMHO your
reply would have be more clear if you'd made clear you also intended for
him to change the class he was using to write the XML.

Pete
 
Back
Top