Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

It all depends on what you are going to use the XML for.

 

For storing data and program settings, XML serialization allows

easy changing between a class instance and an XML file without

you having to write code to parse the file. It's like an INI on

steroids.

 

For transferring data, XML proves useful because it is self-

describing, that is, any client (user or app) can open up an XML file and

understand its contents without having knowledge about how

the file is created or how it should be accessed. If you handed

someone a text file with some lines of statements, they would

have no idea what each line stood for and how to handle it.

 

Check out these two pages on W3Schools for more info:

http://www.w3schools.com/xml/xml_whatis.asp

http://www.w3schools.com/xml/xml_usedfor.asp

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

I see one major downfall of text files and that is that I need to create new StreamReaders every time I read the file, for example, if I wanted to read file "a" I would create a new instance of StreamReader and usually .ReadToEnd().

 

But if I want to read the same file again with the same stream it will not read. If I had 100 settings I would need to create 100 new StreamReaders, all with different names.

 

Is it the same with xml?

C#
  • *Experts*
Posted

I don't see why you need to declare a new StreamReader every

time... are you changing the file in between reading the 100

settings?

 

If you are changing the file, then yes, you will need to declare a

new class to read it every time, whether it be an XML deserializer

or a StreamReader. Otherwise, you should be able to read the

entire file at once.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

I had a time where I created a StreamReader called SR and used it like so:

 

string a=SR.ReadToEnd();

and then in the same file tried to use it again the same way:

string b=SR.ReadToEnd();

but the second time, it never read the file and therefore put nothing in b.

C#
  • *Experts*
Posted

Why couldn't you just set SR.ReadToEnd() to a variable, and then

set every other variable after that to the new variable?

 

string textFile = SR.ReadToEnd();

string a = textFile;
string b = textFile;

 

I should also point out that ReadToEnd is relatively slow, since it

reads the stream one byte at a time. Instead, use Read() or

ReadBlock(), and read the file into a Byte array all at once, and

then you can convert it to a stringwith the System.Text.Encoding class.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

Well, if you really insist on calling the ReadToEnd() method 100

times, you actually can call it more than once.

 

The Stream that the StreamReader is reading has a Position

property, which gets or sets where the Stream is read from. After

calling ReadToEnd(), set the Stream's Position property to 0, and

then you can call ReadToEnd() again. If the Stream class that you

created is not readily accessible, you can refer to it in the

StreamReader's BaseStream property.

 

So...

string a = SR.ReadToEnd();
Stream baseStream = SR.BaseStream;

baseStream.Position = 0;

string b = SR.ReadToEnd();

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...