C#实现获取yaml文件中buildReleases节点下所有的releaseID和buildSystem节点的value值
时间: 2024-09-22 18:07:19 浏览: 48
在C#中,你可以使用`YamlDotNet`库来解析YAML文件。首先,你需要安装这个NuGet包。以下是基本步骤:
1. **安装YamlDotNet库**:
```sh
Install-Package YamlDotNet
```
2. **解析YAML文件**:
使用`YamlDotNet.Core`中的`YamlParser`和`YamlDocument`来读取并解析YAML内容。
```csharp
using YamlDotNet.Core;
using YamlDotNet.RepresentationModel;
// 加载 YAML 文件
string yamlContent = File.ReadAllText("path_to_your_yaml_file.yaml");
var parser = new YamlParser();
var document = parser.Parse(yamlContent);
// 获取 buildReleases 节点下的 releaseID 和 buildSystem 节点的 value
var rootNode = document.RootNode;
if (rootNode is MappingNode mappingRoot && mappingRoot.Children.ContainsKey("buildReleases"))
{
var buildsNode = mappingRoot.Children["buildReleases"];
foreach (var child in buildsNode)
{
if (child.Key == "releaseID")
{
string releaseId = child.Value.ToString(); // releaseID 的值
}
else if (child.Key == "buildSystem" && child.Value is ScalarNode scalarValue)
{
string buildSystemValue = scalarValue.Value; // buildSystem 节点的 value
}
}
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)