What is the best way to store XML data used in a program ? Use RESX file or store it as a .xml file and load and unload the files as per requirement
A third option would be to put the XML file as embedded resource in the assembly. In that case, use Assembly.GetManifestResourceStream() to load the XML.
As Cerebrus wrote, when localization is necessary, RESX would be the way to go.
.resx files are XML files albeit conforming to a particular schema (Microsoft ResX Schema v2.0). This schema was designed with the explicit aim of being easily human readable and editable manually.
I see no problem with storing your data as XML files. Basically it depends on the function of the data - If it is localizable resources that you are trying to store, go with the established .resx files. If not, you are free to use your XML with custom schema.
Putting XML (blobs) in .resx
Positives:
Readily available
Negatives:
It would increase the memory load, if the content is big in size. Big-in-size is in comparison to assembly size without content. If an assembly size is 10kb and content size is 10kb, even though 10kb is not bigger in today's scenario, one can reduce assembly size to half just by keeping content in separate file.
Code manageability goes to down drastically, by keeping XML as string Key-value-pairs in
RESX. As resx editor is not be XML sensitive.
If one is very keen keeping xml content as embedded resource, you can create XML file and keep it as file resource inside RESX.
Related
My goal is to efficiently and simply save tags belonging to image files in a directory. The tags should be stored inside a file that is created.
Let's say in the directory, there is a file 'duck.jpg'. Then p.e. I would like to assign tags 'animal' and 'bird' to this file. The tags are assigned with an image slider, where you tick off checkboxes, and then the tags should be associated with the files.
My question is what data structure / file format would be optimal for this problem. I thought about JSON, XML etc.
The resulting file should not be too big, as tags for many images will be stored, and it must be loadable quickly again and extendable (e.g. adding a new file to the structure should be possible).
What approach would be best suited for the problem?
I have a .NET executable and I need to view the resources attached to it. I extracted .resource file from .NET executable using DotNetResourcesExtract utility but I don't now how to view content of .resource file.
Could someone explain how to view this file?
Not sure you're using it correctly...
Assuming your storing images there.
You can simply do:
Image resfile = ProjectName.Properties.Resources.resourceName;
So if resfile is an image, you can put it into an Image control.
so, if you have an image control on your form you can simply do:
imageControl.Image = ProjectName.Properties.Resources.resourceName;
If it's a text file or any other type of file - again, you can access it the same way. If it's a binary file, the ProjectName.Properties.Resources.resourceName will be a byte array, so you 'll need to load it in the correct manner.
Is that what you're wanting? Otherwise indicate what type of file are you trying to extract from your resource file.
According to MSDN The .resx (XML-based resource format) files are
converted into common language runtime binary .resources files
that can be embedded in a runtime binary executable or compiled into
satellite assemblies.
Getting to the point: How to view this?
Well, Since it's a binary file which contains resource(images etc.) therefore you could always use Windows resources editing/extracting applications.
eg. Restorator, Resource Hacker to name a few.
Meanwhile, have look at this Stackoverflow post. which sound almost similar.
Heading ##I'd like to add a few dictionaries as an embedded resources into my C# solution (*.dic and *.aff files).
Each dictionary is a simple text file inside, so it can be compressed very well.
Is it efficient to store these dictionaries in a *.zip archive, include archive as an embedded resource into my solution and then extract my dictionaries from archive in runtime? Or are embedded resources compressed by default in an assembly file?
By efficient I meant that the install size would be smaller, and runtime slowdown would not be critical.
Yes.
We can see this by adding a 1MB xml file as an embedded resource. The resulting dll will increase by approx 1MB. If we on the other hand zip it before hand, the resulting dll can increase as little a few kb depending on the compression level of the content.
I have a .xml file, which is the result of an export from a non-relational database.
In my application, as a configuration, a user should be able to select nodes from a xml structure, which would be compared/evaluated later on when he upload the .xml.
I have thought to 2 options, but am not very pleased with both:
option1: the structure of the .xml file has to be stored in the database, so the application can display a TreeView, and the user would simply select the nodes to inspect.
(cons: I have many .xml files, one for each system it relates too and each .xml is quit large)
option2: the user would have to upload a .xml file before starting his configuration, so that its structure would be dynamically generated.
(cons: User has one more step to do in order to make his configuration. The one .xml file that he uploads may not contains all the nodes it could have)
Or maybe there are different ways than displaying a TreeView for that purpose ? As it is hard for me to think out of the box, I can't see other options.
I hope this is clear enough,
Maybe there is a kind of best practices I have missed, I am open to suggestions.
I would go with an intermediate option.
Generate dynamically the xsd schemes corresponding to your xml files and store them somewhere.
Process all the xml files in order to have the most complete xsd, because you said that each xml may not contain the whole structure.
I would like to embed a .txt file into my C# project storing a list values for the user. These values should be configurable and therefore the .txt will have to be edited during runtime. I have found out that Embedded Resources cannot be modified. Is there any other way to do that?
Thank you.
Store the text file as an embedded resource. The first time your program is run, copy the embedded resource to a file on disk, and use it for the configuration. Your users can edit the disk file.
The embedded resource version serves as a default configuration.
You can use your app.config or web.config configuration files.
Normall if you will use large amout of data using a database is recommended. I assume you really need just a .txt document. In your assembly write a procedure that will create that text file if its not present. To be more specific lets say your program is mainProgram.exe. In onload event of mainprogram.exe write a procedure checkTxtFile(). This procedure first will check if there is a txt file in the directory.If the file is not presend it will create it with the desired values.
You can create a XML file using Filestream and using it during runtime or
make your own protocol format and store the data in the file so that no one can change it.
Over to that you can also encrypt and decrypt the file on the fly.
Lists of modifyable, persistable values are best stored in a database. There are many lightweight options to choose from; in this case I think a SqlLite database would suit your purposes best.