Why is XML better than just a bunch of Text files?

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
Why is XML better than just a bunch of Text files? Text files will take up less space in memory and I think are easier to open.
 
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. Its 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
 
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?
 
I dont 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.
 
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.
 
Why couldnt you just set SR.ReadToEnd() to a variable, and then
set every other variable after that to the new variable?

C#:
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.
 
Thank you. I could not use another variable becuase they were not known to each other. But I am right though about not being able to use SR.ReadToEnd() again, right?
 
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 Streams 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
StreamReaders BaseStream property.

So...
C#:
string a = SR.ReadToEnd();
Stream baseStream = SR.BaseStream;

baseStream.Position = 0;

string b = SR.ReadToEnd();
 
Thank you. That help a lot. I am probably going to use xml anyway but I will document that fact you just gave me.
 
Back
Top