473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading from XML to MemoryStream

Hello,

I am trying to read from an xml file and put it to a memory stream so I can
read it multiple times from the beginning using XmlTextReader without
accessing the harddisk , I tried using FileStream but it locked the xml file
so I can't access it anymore until I close the filestream, so my question is
how to read from the file to the memorystream directly or read to the
filestream first and the copy it to the memorystream?

Thanks
Yehia

Sep 30 '06 #1
3 3573
Hi Yehia,

You might want to try loading the xml file into an XmlDocument instead of parsing it multiple times using a reader.
But if you want to use a MemoryStream then here's some code:

string file = @"C:\file.xml";

// .NET 2.0
byte[] bytes = System.IO.File.ReadAllBytes(file);

/* .NET 1.*
byte[] bytes = null;
using (System.IO.FileStream fileStream = new System.IO.FileStream(
file, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
System.IO.BinaryReader reader = new System.IO.BinaryReader(fileStream);
bytes = reader.ReadBytes((int) fileStream.Length);
}
*/

using (System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes))
{
System.Xml.XmlTextReader reader = System.Xml.XmlTextReader.Create(stream);
...
}

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:7A**********************************@microsof t.com...
Hello,

I am trying to read from an xml file and put it to a memory stream so I can read it multiple times from the beginning using
XmlTextReader without accessing the harddisk , I tried using FileStream but it locked the xml file so I can't access it anymore
until I close the filestream, so my question is how to read from the file to the memorystream directly or read to the filestream
first and the copy it to the memorystream?

Thanks
Yehia

Oct 1 '06 #2
but I read that XmlDocument is slow for large documents so I used
XmlTextReader,
what does "large" mean anyway? 1 MB, 100 MB, 500 MB?
my xml document is 1.5 Mb and has about 6000 element, is this considered
big? will be any performance hit using XmlDocument with this size of
document?
and another irrelevant question
string file = @"C:\file.xml"; --what does @ mean

Oct 2 '06 #3
Hi Yehia,

Well I would suggest that you use XmlTextReader to save memory and only switch to XmlDocument if you start experiencing performance
problems that you can be sure are caused by multiple iterations of your source xml.
@ symbol in C# tells the compiler to treat the following string as a literal, without escapes.

"Hello\n" // = Hello{newline}
@"Hello\n" // = Hello\n
"Hello\"" // = Hello"
@"Hello\"" // won't compile because \ no longer escapes the "
@"Hello""" // = Hello" (double-quote becomes a single quote)

One great thing about @ is that you can use it on strings to span multiple lines in code and preserve the new lines:

string multiline = @"First Line
Second Line
Third Line";

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:6F**********************************@microsof t.com...
but I read that XmlDocument is slow for large documents so I used XmlTextReader,
what does "large" mean anyway? 1 MB, 100 MB, 500 MB?
my xml document is 1.5 Mb and has about 6000 element, is this considered big? will be any performance hit using XmlDocument with
this size of document?
and another irrelevant question
string file = @"C:\file.xml"; --what does @ mean

Oct 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cnu | last post by:
Hi I have to write images(.gif/.bmp/.jpg/.ico), to db and read them. Uploading images to db works fine for me. Reading from db to byte is also ok. But, when I try to display them in my form...
1
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
5
by: Marco Castro | last post by:
Along time ago I created an access database where I used the ole field type to store images. If I look at the table data it has the work Picture in its field. The image in that field displays...
5
by: ltt19 | last post by:
Hi, I want to read line by line in a variable, but it could not be foward only , it need to be forward and backward, it also needs to say me which line it is reading. I do not know if it is...
3
by: cartoper | last post by:
I am getting a packet of data from an instrument. The strings in the data packet are null terminted. What is the best way to extract a string from the data packet? I am working in .Net 20. ...
1
by: chinthamani | last post by:
Hai I want to open a file and read when the same file is already opened for writing. I need to do the reading process simultaniously when the file is being writen. Regards. S.Vinodh Noel...
9
by: peter.bremer | last post by:
Hi all, I've got a SQL Server database that contains zipped information stored in (binary) image fields. To complicate things, this zipped data is combined with plain-text data. I've verified...
3
by: Willy Stevens | last post by:
Hello, In my application I have to read sometimes quite big chunk of binary data. I have a buffer which default size is 32000 bytes. But how could I read binary data that exceeds 32000 bytes?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.