一文件内容语法
1.ini语法
特点 :最古老、最简单,只有 键=值 + 分组后缀 :.ini
语法规则
- 用
[分组名]表示一节 - 用
key=value存数据 ;开头是注释- 没有嵌套、没有数组
cs
; 这是注释
[Database]
Server=127.0.0.1
Database=TestDB
UserId=sa
[AppSet]
LogPath=Logs/
AutoRun=True
优点 / 缺点
✅ 超级简单
❌ 不能存复杂结构(不能嵌套、不能列表)
2.xml语法
特点 :用标签包裹,像 HTML,能嵌套、能加属性后缀 :.xml
语法规则
- 必须有根节点
- 格式:
<节点>内容</节点> - 可以嵌套
- 可以加属性
<!-- 注释 -->
示例
XML
<!-- XML注释 -->
<Config>
<Database>
<Server>127.0.0.1</Server>
<Database>TestDB</Database>
</Database>
<AppSet LogPath="Logs/" AutoRun="True" />
</Config>
优点 / 缺点
✅ 结构强、可嵌套、可属性
❌ 语法啰嗦、冗余多
3.JSON文件
特点 :轻量、简洁、前后端通用后缀 :.json
语法规则
- { } 表示对象
- [ ] 表示数组 / 列表
key: value- 字符串必须用 双引号
- 逗号分隔,最后一项不能加逗号
- 不能写注释(标准 JSON 不支持)
示例
cs
{
"Database": {
"Server": "127.0.0.1",
"Database": "TestDB"
},
"AppSet": {
"LogPath": "Logs/",
"AutoRun": true
},
"UserList": [
{ "Name": "张三", "Age": 20 },
{ "Name": "李四", "Age": 21 }
]
}
4.对比总结
| 格式 | 结构符号 | 注释 | 嵌套 | 数组 | 主流程度 |
|---|---|---|---|---|---|
| INI | [ ] key=val |
; |
❌ 不支持 | ❌ 不支持 | 老项目用 |
| XML | <标签> |
<!-- --> |
✅ 支持 | ❌ 麻烦 | 老框架用 |
| JSON | { } [ ] |
标准不支持 | ✅ 支持 | ✅ 支持 | 🔥 现在首选 |
二命名空间依赖
| 文件类型 | 命名空间 | 说明 |
|---|---|---|
| XML | System.Xml.Linq |
现代、简洁、LINQ 支持 |
| JSON | System.Text.Json |
.NET 官方自带 |
| JSON | Newtonsoft.Json |
最流行、兼容性强 |
| INI | System.Runtime.InteropServices |
调用 Windows API |
| INI | IniParser |
开源库,最简单 |
三文件解析
1.XML解析
cs
using System;
using System.Xml.Linq;
// 读取 XML
var xml = XDocument.Load("test.xml");
string name = xml.Element("Root")?.Element("Name")?.Value;
int age = int.Parse(xml.Element("Root")?.Element("Age")?.Value);
// 写入 XML
var doc = new XDocument(
new XElement("Root",
new XElement("Name", "张三"),
new XElement("Age", 20)
)
);
doc.Save("out.xml");
2.JSON解析
cs
using System;
using System.Text.Json;
// 定义类
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
// 读取 JSON
string json = System.IO.File.ReadAllText("user.json");
User user = JsonSerializer.Deserialize<User>(json);
// 写入 JSON
User u = new User { Name = "李四", Age = 25 };
string jsonStr = JsonSerializer.Serialize(u, new JsonSerializerOptions { WriteIndented = true });
System.IO.File.WriteAllText("out.json", jsonStr);
3.INI解析(调用Windows API)
C# 没有内置 INI 类,直接调用系统 kernel32 最简单:
cs
using System;
using System.Runtime.InteropServices;
public static class IniHelper
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] ret, int size, string filePath);
// 读
public static string Read(string section, string key, string path)
{
byte[] buffer = new byte[1024];
int len = GetPrivateProfileString(section, key, "", buffer, buffer.Length, path);
return System.Text.Encoding.UTF8.GetString(buffer, 0, len);
}
// 写
public static void Write(string section, string key, string value, string path)
{
WritePrivateProfileString(section, key, value, path);
}
}
// 使用
IniHelper.Write("Config", "Name", "小明", "config.ini");
string name = IniHelper.Read("Config", "Name", "config.ini");