aewarnick Posted February 24, 2003 Posted February 24, 2003 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. Quote C#
*Experts* Bucky Posted February 24, 2003 *Experts* Posted February 24, 2003 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 Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 24, 2003 Author Posted February 24, 2003 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? Quote C#
*Experts* Bucky Posted February 24, 2003 *Experts* Posted February 24, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 24, 2003 Author Posted February 24, 2003 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. Quote C#
*Experts* Bucky Posted February 24, 2003 *Experts* Posted February 24, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 24, 2003 Author Posted February 24, 2003 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? Quote C#
*Experts* Bucky Posted February 25, 2003 *Experts* Posted February 25, 2003 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(); Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 Thank you. That help a lot. I am probably going to use xml anyway but I will document that fact you just gave me. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.