473,698 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3603
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.xm l";

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

/* .NET 1.*
byte[] bytes = null;
using (System.IO.File Stream fileStream = new System.IO.FileS tream(
file, System.IO.FileM ode.Open, System.IO.FileA ccess.Read))
{
System.IO.Binar yReader reader = new System.IO.Binar yReader(fileStr eam);
bytes = reader.ReadByte s((int) fileStream.Leng th);
}
*/

using (System.IO.Memo ryStream stream = new System.IO.Memor yStream(bytes))
{
System.Xml.XmlT extReader reader = System.Xml.XmlT extReader.Creat e(stream);
...
}

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmai l.comwrote in message news:7A******** *************** ***********@mic rosoft.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.xm l"; --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*****@hotmai l.comwrote in message news:6F******** *************** ***********@mic rosoft.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.xm l"; --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
2653
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 controls, I get this error. This is how it goes : <code> byte bImage1 = (byte)datasetImageList.Tables.Rows; System.IO.MemoryStream ms = new MemoryStream(bImage1, 0, bImage1.Length); this.lblRunImage.Image = System.Drawing.Image.FromStream(ms); //...
1
11172
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
9508
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
5
315
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 properly if I display it from inside of access but I can't figure out how to convert what's in there to anything useful outside of access. Is there some way to read what's in there from a .net application? I would be content with just being able...
5
1715
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 possible. Thanks in advance ltt19
3
2135
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. Presently the code reads byte by byte through the packet adding the value into a List<byte>. The a new byte is created based on the count of the List<byte>. The code iterates though the List<byte> to populate the byte. Finally the byte is...
1
1401
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 Software Engineer (Bells Softech Limited) Bangalore.
9
4283
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 the zipped data to be readable by SharpZipLib by writing the field contents to a file and using a binary editor to manually strip the plaintext data in front of it.
3
5385
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? Is allocating more size to buffer and copy data that exceeds 32000 bytes at tail the only way or are there any other possibilities?
0
8668
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9014
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8855
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.