在.NET中最简单的键/值对文件进行解析最简单、文件、NET

2023-09-04 10:47:58 作者:囬憶ヽ逆流成河

我的项目需要一个文件,在那里我将存储键/值对数据应该能够读取和被用户修改。我想只是希望的钥匙在那里的程序,我想从文件中快速分析他们越好。

我可以将它们存储在XML,但是XML是方式复杂,它需要遍历节点,和子节点等等,我要的是一些类,需要一个文件,并生成键值对。我希望尽可能少的错误处理越好,我希望它用尽可能少code越好。

完成

我可以codeA类像我自己,但我宁愿了解它是如何don'e比造车轮两次框架。是否有一些内置的魔法类。NET(3.5)有能力这样做呢?

  MagicClass KV =新MagicClass(的Settings.ini); //它不neccesarily必须是一个INI文件,它可以是任何简单的键/值对格式。
弦值1 = kv.get(KEY1);
...
 

解决方案

使用 KeyValuePair 类你键和值,然后只序列化列表磁盘与 XMLSerializer的。

这是最简单的方法,我觉得。你不会担心遍历节点。调用反序列化功能会为你做的。用户可以,如果他们还希望在文件中编辑值。

一键自动整理文件 简单好用的Python脚本

My project requires a file where I will store key/value pair data that should be able to be read and modified by the user. I want the program to just expect the keys to be there, and I want to parse them from the file as quickly as possible.

I could store them in XML, but XML is way to complex, and it would require traversing nodes, and child nodes and so on, all I want is some class that takes a file and generates key value pairs. I want as little error handling as possible, and I want it done with as little code as possible.

I could code a class like that myself, but I'd rather learn how it's don'e in the framework than inventing the wheel twice. Are there some built in magic class in .NET (3.5) that are able to do so?

MagicClass kv = new MagicClass("Settings.ini"); // It doesn't neccesarily have to be an INI file, it can be any simple key/value pair format.
string Value1 = kv.get("Key1");
...

解决方案

Use the KeyValuePair class for you Key and Value, then just serialize a List to disk with an XMLSerializer.

That would be the simplest approach I feel. You wouldn't have to worry about traversing nodes. Calling the Deserialize function will do that for you. The user could edit the values in the file if they wish also.