Creating an XML file in temporary memory

  • Thread starter Bharathi Harshavardhan
  • 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?
 
M

Marc Gravell

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
 
D

Duggi

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
 
J

Jon Skeet [C# MVP]

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
 
P

Peter Duniho

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
 
P

Peter Duniho

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
 
M

Marc Gravell

By which I meant:

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

Marc
 
B

Bharathi Harshavardhan

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.
 
P

Peter Duniho

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top