简体   繁体   中英

Need some advice for project

I'm going to do a project task as the last part of a programming course in C#, and I have choosen to do a media library for storing information of DVD movie collections. The thing I need some advice is about how to save this information since the task should be solved by using simple text files and not databases which had been better.

So, which is the best way to save this information to be able to add, edit and allow search for titles and actors and also sort after genre to mention some of the idéas I have for this program.

Should I just save the information by just add the title, year, playtime, director, actor1, actor2, actor3, genre, grade, comment for each movie lika a row at the end of the file? Can I search in the file or should I, in some way, first read it all into an array, and then do the serach and perhaps edit and save the complete file again?

Or is it better to use XML like this:

<movie_collections>
<movie>
<title=Tron Legacy"></title>
<year=2010></year>
<playtime=120></playtime>
etc.
</movie>
</movie_collections>

If I use XML, can I search for a title and just load or edit that part? Are there better alternatives than these?

You may store the data in XML file. An XML file can store data similar to a database table. You can add multiple records, hierarchical data etc... You may easily query the data using LINQ to XML .

If you dont want to use LINQ to XML , You can use so typical XMLDocument to handle the XML data.

Maybe you're approaching this at too low a level. The file is realistically just for persistance rather than implement your own DB as a file.

Just implement the object that you're after and serialize it out imo.

public class MovieCollections
{
    private ICollection<Movie> Movies {get; set;}
    // etc...
}

public class Movie
{
    public string Title {get; private set;}
    public int PlayTime {get; private set;}
    public DateTime ReleaseDate {get; private set;}
}

Serialize instance of MovieCollections. Keep MovieCollections perhaps as a Singleton instance since you only want one collection.

By the way this seems to be very general question & a quite common homework question too!!

I think you should google for this, you will get better projects ideas.

Something similar on this lines :

1. Simple Movie Database in C# using Microsoft Access
2. Create a Movie Database Application
3. imdbapi
& finally
4. SO's similar post

And as far as comparison between a database & XML is concern, I did recommend you a database because several pros over XML as far such type of project is considered.

As @M_Affifi suggests, think of all your interaction with your data (eg sort, add, find, delete, update, etc) through your objects which reside in memory (eg Instance of MovieCollections class and instances of Movie class), the XML file is only used to store the state of your objects, so you have that data available next time you run your application.

Once you're done manipulating your data just serialize it to XML. Serialization is just the process to convert your objects into a format that you can store or transfer, in this case will be conversion to XML. Take a look at this reference , Your serialization needs are very basic, so you'll need just few lines of code.

System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(YourObject.GetType());
      x.Serialize(Console.Out, YourObject)

Finally, I would suggest thinking of your application logic. When program starts you may want to check if your Movies.xml exists, if so, then 'Deserialize' it, which means loading it from XML to your object collection. If XML file doesn't exist then just start manipulating your objects and when you're done give the option to Save(Serialize). I can help with the serialization/deserialization process if needed.

Hope this is helpful ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM