在yaml中解析评论

时间:2019-02-15 12:33:24

标签: c# yaml yamldotnet

我需要解析包含注释的yaml文档,我的yaml文件看起来像这样

Prerequisite_Services:
- Service_Name: abc
Workflows:
- Workflow: workflow1
  Service:
  - Service_Name: abc1
  - Service_Name: abc2
# - Service_Name: abc3
- Workflow: xyz
  Service:
  - Service_Name: def1
  - Service_Name: def2
  - Service_Name: def3
  - Service_Name: def4
# - Service_Name: def5
  - Service_Name: def6

我需要同时解析服务abc3和def5 我尝试了以下代码

using System;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace YamlTesting
{
public class ConfigReader
{
    private static string _filePath = string.Empty;
    private static Workflows _workFlows = null;

    public static void Main()
    {
        _filePath = "D:\\testdata.yaml";
        if (!File.Exists(_filePath))
        {
            throw new FileNotFoundException("Missing File : Check the source folder for Workflow file.");
        }
        try
        {
            using (var input = File.OpenText(_filePath))
            {
                var parser = new Parser(new Scanner(new StreamReader(_filePath), skipComments: false));
                var deserializer = new DeserializerBuilder()
                    .WithNamingConvention(new CamelCaseNamingConvention())
                    .Build();
                _workFlows = deserializer.Deserialize<Workflows>(parser);
            }
        }

        catch (SyntaxErrorException e)
        {
            throw new Exception("Sytanx Error : Check the syntax in the Workflow file.", e);
        }
        catch (YamlException e)
        {
            throw new Exception("Invalid workflow identified. Check the Workflow file.", e);
        }
    }
}

public class Workflows
{
    [YamlMember(Alias = "Prerequisite_Services", ApplyNamingConventions = false)]
    public List<Servicess> PreRequisiteServices { get; set; }

    [YamlMember(Alias = "Workflows", ApplyNamingConventions = false)]
    public List<WorkFlow> WorkFlows { get; set; }
}

public class WorkFlow
{
    [YamlMember(Alias = "Workflow", ApplyNamingConventions = false)]
    public string WorkflowName { get; set; }
    [YamlMember(Alias = "Service", ApplyNamingConventions = false)]
    public List<Servicess> Service { get; set; }
}

public class Servicess
{
    [YamlMember(Alias = "Service_Name", ApplyNamingConventions = false)]
    public string ServiceName { get; set; }
}

运行上面的代码“获取注释而不是标量”时出现错误, 我如何阅读评论?我需要此注释,因为这是配置文件,我需要替换它,并且我不能忽略注释

2 个答案:

答案 0 :(得分:1)

我无法解析注释,因此我添加了以下代码,我在另一个文件讨论中找到了该代码:

public void AddNotParsedData (string fich)
{
      string[] full_file = File.ReadAllLines(fich);
      List<string> l = new List<string>();
      l.AddRange(full_file);

      l.Insert(0, "Comment1");
      l.Insert(9, "Comment2");
      
      File.WriteAllLines(fich, l.ToArray());
}

这不是动态的,但它是一种解决方法,可以在 yaml 始终具有相同格式的情况下使用。

答案 1 :(得分:0)

反序列化器不知道如何处理注释。如果您确实需要保留评论,则必须直接使用IParser解析流。您可能会扩展反序列化器以能够识别注释,但是您会遇到一个问题,那就是它需要一些地方才能将它们存储在某个地方。