C#实现获取yaml文件中所有的releaseID和buildSystem的value值
时间: 2024-09-22 22:07:03 浏览: 32
在C#中,要从YAML文件中提取所有`releaseID`和`buildSystem`的值,你可以使用一些第三方库如`YamlDotNet`或者`.NET Core`自带的`System.Text.Json`处理YAML文件。首先,需要安装`YamlDotNet`库:
```sh
Install-Package YamlDotNet
```
然后,你可以编写这样的代码片段来读取和解析YAML文件:
```csharp
using System;
using System.IO;
using YamlDotNet.Reading;
public class YamlParser
{
public static void ExtractReleaseAndBuildInfo(string yamlFilePath)
{
if (!File.Exists(yamlFilePath))
{
throw new FileNotFoundException($"YAML文件不存在: {yamlFilePath}");
}
var stream = File.OpenText(yamlFilePath);
using (var reader = new YamlStream())
{
try
{
reader.Load(stream);
foreach (var document in reader.Documents)
{
foreach (var node in document.RootNode.Nodes)
{
if (node is MappingNode mapping && mapping.Children.ContainsKey("releaseID") && mapping.Children.ContainsKey("buildSystem"))
{
var releaseId = mapping Children["releaseID"].Value.ToString();
var buildSystem = mapping.Children["buildSystem"].Value.ToString();
Console.WriteLine($"releaseID: {releaseId}, buildSystem: {buildSystem}");
}
}
}
}
catch (YamlException ex)
{
Console.WriteLine($"解析YAML文件时出错: {ex.Message}");
}
}
}
}
// 调用函数
string yamlFilePath = "path_to_your_yaml_file.yaml";
YamlParser.ExtractReleaseAndBuildInfo(yamlFilePath);
```
阅读全文