![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 894
| Reading an XML file Code: <root>
<course="Math">
<book>
<author>
amélie nothomb
</author>
<title>
métaphysique des tubes
</title>
<isbn>
</isbn>
<price>
10.45
</price>
</book>
<evaluation>
<title>
recherche
</title>
<note>
70.3
</note>
<max>
100.0
</max>
<group>
65.4
</group>
<weight>
20
</weight>
</evaluation>
</course>
</root>
Code: using System;
using System.IO;
using System.Xml;
class MyApp
{
public static void Main()
{
FileStream fs = new FileStream("test.xml", FileMode.Open);
XmlReader xml_reader = new XmlTextReader(fs);
while(!xml_reader.EOF) Console.WriteLine(xml_reader.ReadString());
Console.Read();
}
}
Edit: It seems like the application isn't doing anything yet the only way to close the program is through CTRL + C. |
| Desolation is offline | |
| | #2 | |
| Anti-Poster Join Date: Feb 2002
Posts: 1,241
| I can answer this two different ways. First, the helpful answer: Quote:
Code: [Serializable]
class Test
{
public string _name;
public int _number;
public double _price;
public void Print()
{
Console.WriteLine("Name: {0}", _name);
Console.WriteLine("Number: {0}", _number);
Console.WriteLine("Price: {0}", _price);
}
}
class Program
{
static void Main(string[] args)
{
Test test = new Test();
test._name = "My name";
test._number = 42;
test._price = 3.141592;
Console.WriteLine("Before serialization the object contains: ");
test.Print();
//SoapFormatter formatter;
BinaryFormatter formatter;
//Opens a file and serializes the object into it in binary format.
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
//formatter = new SoapFormatter();
formatter = new BinaryFormatter();
formatter.Serialize(stream, test);
}
//Empties obj.
test = null;
//Opens file "data.xml" and deserializes the object from it.
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
//formatter = new SoapFormatter();
formatter = new BinaryFormatter();
test = (Test)formatter.Deserialize(stream);
}
Console.WriteLine();
Console.WriteLine("After deserialization the object contains: ");
test.Print();
Console.WriteLine("Finished");
Console.ReadLine();
}
}
Code: using (FileStream fs = new FileStream("test.xml", FileMode.Open))
{
using (XmlReader xml_reader = new XmlTextReader(fs))
{
while (!xml_reader.EOF)
Console.WriteLine(xml_reader.ReadString());
}
}
Console.Read();
[edit] Sorry for all the text, but I figured I'd better make my 1,000th post worth something.
__________________ Rule #1: Every rule has exceptions | |
| pianorain is offline | |
| | #3 |
| Registered User Join Date: May 2006
Posts: 894
| Ok well, here's how I've structured my code up to now. I know it may be not top-notch C# code but it does the job. (I've stripped a lot of it so that it is shorter and to-the-point). Code: /*
* Created by SharpDevelop.
* User: Alexandre
* Date: 2006-08-15
* Time: 15:17
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
namespace StudentData
{
class Book
{
public String Title, ISBN, Author;
public float Price;
}
class Evaluation
{
public String Name;
public float Note, MaxScore, GroupAvg, Weight;
}
class Course
{
public void AddBook(Book b)
{
BookList.Add(b);
}
public void AddBook(String t, String a, String i, float p)
{
BookList.Add(new Book(t, a, i, p));
}
public void AddEvaluation(Evaluation ev)
{
EvaluationList.Add(ev);
}
public void AddEvaluation(String na, float no, float m, float g, float w)
{
EvaluationList.Add(new Evaluation(na, no, m, g, w));
}
private String Name, Id;
private List<Evaluation> EvaluationList;
private List<Book> BookList;
}
class StudentManager
{
public void GetDataFromFile(String file_name)
{
// This is where I want to read the file
}
private List<Course> CourseList;
}
}
Thanks =] |
| Desolation is offline | |
| | #4 | |
| and the Hat of Clumsiness Join Date: Oct 2002
Posts: 1,101
| Quote:
Thats how its supposed to be, although like I said I've never attempted to do this. I've only used the xmlSerializer to serialize objects, write them to files to later deserialize them. But still you can try it, and create an object, serialize, check output and start funetuning, but maybe you'r better of writing your own serializer in the end , since development time could be less. | |
| GanglyLamb is offline | |
| | #5 |
| Anti-Poster Join Date: Feb 2002
Posts: 1,241
| Actually, making use of the normal .Net serialization is a cinch. In the OP's case, all he has to do is mark all of his classes as Serializable. To save data to the file, make a formatter and call Serialize with the CourseList as the argument. To load data from the file, make a formatter and call Deserialize, casting and assigning CourseList from the return type. The nice thing about normal serialization is that you don't have to have public getters for all the things you want to save; normal serialization serializes everything, including private stuff.
__________________ Rule #1: Every rule has exceptions |
| pianorain is offline | |
| | #6 |
| Registered User Join Date: May 2006
Posts: 894
| Thanks a lot guys ! This way I can make modifications to the class objects on runtime and then erase the old file and put the new content in ! Thank you guys so much =] |
| Desolation is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| opening empty file causes access violation | trevordunstan | C Programming | 10 | 10-21-2008 11:19 PM |
| Formatting a text file... | dagorsul | C Programming | 12 | 05-02-2008 03:53 AM |
| Formatting the contents of a text file | dagorsul | C++ Programming | 2 | 04-29-2008 12:36 PM |
| Possible circular definition with singleton objects | techrolla | C++ Programming | 3 | 12-26-2004 10:46 AM |
| System | drdroid | C++ Programming | 3 | 06-28-2002 10:12 PM |