Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Creating an XML file in temporary memory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bharathi Harshavardhan - 28 Jun 2007 07:58 GMT
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?

---
Bharathi
Marc Gravell - 28 Jun 2007 08:03 GMT
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
Peter Duniho - 28 Jun 2007 08:38 GMT
> 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
Marc Gravell - 28 Jun 2007 08:58 GMT
By which I meant:

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

Marc
Bharathi Harshavardhan - 28 Jun 2007 09:37 GMT
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:
>
[quoted text clipped - 6 lines]
>
> Marc
Peter Duniho - 28 Jun 2007 18:12 GMT
> 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
Duggi - 28 Jun 2007 08:16 GMT
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

On Jun 28, 11:58 am, Bharathi Harshavardhan <bharathi8...@gmail.com>
wrote:
> Hi,
>
[quoted text clipped - 12 lines]
> ---
> Bharathi
Jon Skeet [C# MVP] - 28 Jun 2007 08:38 GMT
On Jun 28, 7:58 am, Bharathi Harshavardhan <bharathi8...@gmail.com>
wrote:
> I have a requirement where I need to create an xml file in temporary
> memory and not on hard disk.
[quoted text clipped - 7 lines]
>
> 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
Peter Duniho - 28 Jun 2007 08:46 GMT
> 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
Marc Gravell - 28 Jun 2007 08:59 GMT
explained above

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.