顺便贴一下代码,很垃圾但可以运行
//本示例演示了 loadstream 功能
using System;
using System.Text;
using
System.IO;
using System.Collections.Generic;
using YamlDotNet.RepresentationModel;
namespace YamlDotNet.Samples
{
public class LoadYamlStream
{
public static void Main()
{
// Setup the input
var input = new StringReader(Document);
// Load the stream
var yaml = new YamlStream();
yaml.Load(input);
// Examine the stream
var mapping =
(YamlMappingNode)yaml.Documents[0].RootNode;
PrintValues(mapping);
//foreach (var entry in mapping.Children)
//{
// Console.WriteLine((YamlScalarNode)entry.Key);
//}
//// List all the items
//var items = (YamlSequenceNode)mapping.Children[new YamlScalarNode("items")];
//foreach (YamlMappingNode item in items)
//{
// Console.WriteLine(
// "{0}\t{1}",
// item.Children[new YamlScalarNode("part_no")],
// item.Children[new YamlScalarNode("descrip")]
// );
//}
}
private static void PrintValues(YamlMappingNode mapping,string level="")
{
foreach(var entry in mapping.Children)
{
#nullable enable
string? key = ((YamlScalarNode)entry.Key).Value;
if(key == null)
{
throw new Exception("错误");
}
//先输出键
Console.Write(level+((YamlScalarNode)entry.Key).Value+":");
//判断是否是字符串,即是否到达了底部
var child = mapping.Children[new YamlScalarNode(key)];
if (child.NodeType==YamlNodeType.Scalar)
{
Console.WriteLine(((YamlScalarNode)child).Value);
}//判断节点值是否是 mapping,即是否嵌套
else if (child.NodeType == YamlNodeType.Mapping)
{
Console.WriteLine();
PrintValues((YamlMappingNode)child,level+" ");
}//判断节点值是否是 Sequence,是不是集合
else if (child.NodeType == YamlNodeType.Sequence)
{
Console.WriteLine();
foreach (var value in ((YamlSequenceNode)child))
{
PrintValues((YamlMappingNode)value,level + " ");
}
}
}
level += " ";
}
private const string Document = @"#这是导入文件,通过此文件导入设置以及数据
设置:
开机启动: true
黑夜模式: true
离线模式: false
显示图标: false
#
数据:
- 标题: Visual Code
数据表:
创建数据表: Create database name;
创建表: show create table <表名>;
- 标题: Word
常用操作:
加粗: Ctrl+B
";
}
}