I have a requirement of parsing yaml documents including comments, my yaml file looks like this
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
i need to parse both service abc3 and def5 i tried below codes
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; }
}
I am getting an error when running above code "got comment instead of scalar", how i can read the comment? i need this comments as this is config file and i need to replace it and i can't ignore comments