What would be a good approach to using XML as data storage for a small C # application?

I need some opinions on what would be a good approach to using XML as data storage for a small C # application. This is a standalone application, users do not use the same persistence of data, therefore, access to files is exceptional. This is why I was primarily thinking about XML.

I know my design patterns, so if I write regular layers, I can isolate the save and then change it if necessary. Again, this is a small application, so I need to write it quickly.

Should I just use Linq for XML and relive it? If so, what will be my rewrite efforts if I decide to replace XML with an embedded database? How is Linq performance written to an XML file?

But if I donโ€™t go with Linq, what would you guys suggest?

Update

According to the comments I received, I may need to specify a little more. This is a teacher reporting app. These are my main entities:

  • Student
  • Course
  • Teacher (there should be only one, but I will keep it because of future integration opportunities)
  • Grade (a student may have more than one class in each course)

Now a few questions:

  • Should there be one XML file under the hood for each object?
  • Under the hood should I use Linq for XML? Is there anything else to even reckon with?

And some comments

  • I understand that I must interact with the "instance of the persistence class" using IEnumerable<MyEntity> , on the inputs and outputs. It gives me flexibility.
  • Although I am truly open to suggestions, I really do not want to consider introducing a database right now. This small application is a great opportunity to work and experiment with new things in production (and not just with testing) and a little risk.
+6
source share
3 answers

I suggest you:

  • Define a model (one class) that represents the data you want to save (may be hierarchical)
  • Use serialization for serialization or deserialization in XML or binary or even JSON, if necessary.
+7
source

It might be much easier to use SQL Server Compact. Thus, it will be easier to use the tools of the Entity Framework and, if necessary, subsequently switch to a full SQL Server.

+3
source

XML / JSON works best if you write the entire document right away, an attempt to add data may also be fine, but at some point you probably want to insert / update partial data in the middle of the document / file and that is, when it getting hairy.

If you can live with a solution that reads data in memory (either directly or partially by query), and then at some later point writes everything back to a file, then it works well with a single XML / JSON file. Until you need to periodically update / insert / delete partial data from a file, the file should do this (although performance may be a problem).

One way that I have used many times is to define an XSD schema , including constraints, data types, etc. at your discretion, and then configure the pre-build step, which automatically generates a serializable hierarchy of C # classes for you (using, for example, XSD.exe ). Using normal funcationality XML serialization, then it is very easy to load (and, if necessary, check the XML through your XSD document) the entire document into memory, read / process it (query / update / insert / delete), and then serialize the entire hierarchy of objects back in XML.

See this page for information on XSD.exe .

This approach quickly gives you a pretty useful and solid foundation for development. This should be enough for a while, but you may need to rewrite it a bit if you later move to a database-based solution, if you do not distract access to the data from the inside from the very beginning.

If a clean file-based solution is not enough, go for some kind of database-oriented solution, preferably some kind of built-in database such as SqlLite or BerkeleyDb .

+2
source

Source: https://habr.com/ru/post/886142/


All Articles