开发者

Persisting with XML: multiple files or single file?

I've already about general guidelines on What would be a good approch for using XML as data persistence for a small C# app?.

I decide to use XML under the hood. It's a small app, a report card app for teacher use. These are my main entities:

  • Student
  • Course
  • Teacher 开发者_开发知识库(there should be only one, but I'll store it because of future integration possibilities)
  • Grade (a student can have more than one grade in each course)

I have some points I would like suggestions:

  • Under the hood, should I have one XML file per entity or one big XML file?
    • How's that under a performance perspective
    • How's that under a data joining perspective
  • Under the hood, should I use Linq to XML? Is there something else to even be considered?


One XML file is probably easier.

That file might look like this:

<ReportCardData>
    <Students> 
       <Student>...data for student A ...</Student>
          ...
    </Students>
    <Teachers>
       <Teacher> ...
         ...
    </Teachers>
    <Courses>
       <Course> ...
          ...
    </Courses>
 </ReportCardData>

The code in C# would look like this:

public class ReportCardData 
{
   public List<Student> Students;
   public List<Teacher> Teachers;
   public List<Course> Courses;
}

And the code to de-serialize (read) the data looks like this:

ReportCardData rcd = null;
var s= new System.Xml.Serialization.XmlSerializer(typeof(ReportCardData>));
using(System.IO.StreamReader reader= System.IO.File.OpenText(filepath))
{
    rcd= (ReportCardData) s.Deserialize(reader);
}

...be sure to add in the appropriate exception handling, etc.

Using XML Serialization works fine for something like this, even for large data sets with multiple tens of megabytes. (If you are talking about 100's of Megabytes of data, then maybe consider a real database like SQL Express)

The reading and writing performance will likely be fine. Keep in mind that when you de-serialize data from the XML file, the entire dataset will be held in memory in your app. So if it is 15mb worth of grade data, then it is all in memory at one time.

You also asked about a "data joining perspective" - not sure what that means, but using Linq to XML you can perform queries across that data. The performance of the in-memory queries is also fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜