0

I'm new in using yaml files, and after searched in the web I didn't found a solution yet.

I generate a yaml files and I want to make some indentations like this examples:

My currrent output:

  fields:
  - name: tx_trig
    description: Indicates the start of Tx when the PRT timer expires
    position: [0, 0]
    type: logic
    default: 0
  - name: end_of_line
    description: End of line
    position: [1, 1]
    type: logic
    default: 0
  - name: paramup
    description: Parameter update
    position: [2, 2]
    type: logic
    default: 0

My desire output (indent white spaces before a field node, and blank row after a node:

  fields:
      - name: tx_trig
        description: Indicates the start of Tx when the PRT timer expires
        position: [0, 0]
        type: logic
        default: 0
        
      - name: end_of_line
        description: End of line
        position: [1, 1]
        type: logic
        default: 0
        
      - name: paramup
        description: Parameter update
        position: [2, 2]
        type: logic
        default: 0

Here is a part of the generating code of the yaml file:

YamlSequenceNode fieldsSequence = new YamlSequenceNode();
foreach (Field field in register.Fields)
{
    var fieldNode = new YamlMappingNode();
    fieldNode.Add("name", field.Name);
    fieldNode.Add("description", field.Description);
    fieldNode.Add("position", field.Position);
    fieldNode.Add("type", field.type.ToString());
    fieldNode.Add("default", field.DefaultValue);
    fieldsSequence.Add(fieldNode);
}
registerNode.Add("fields", fieldsSequence);

Thanks for any help.

Orionlk
  • 278
  • 2
  • 3
  • 17

1 Answers1

1

If your YamlDotNet is recent enough, it has this feature which allows you to do

var serializer = new SerializerBuilder()
    .WithIndentedSequences()
    .Build();

As for the empty lines (also asked here), that is not possible in YamlDotNet. Mind that YAML is not a format where you have complete control over the representation, see also this question.

flyx
  • 35,506
  • 7
  • 89
  • 126