I'am able to deserialize the yaml file, consult and change the values.
Due to an requirement, I have to use the minus(-) symbol in front of the variables NAME, VERSION, ...
The file test.yaml
PlatForm: windows
Version: 10
# Software Info
SOFTWARE:
Software-01:
- NAME : MFS2020
- VERSION : 1.12.2015
- Customized : true
Software-02:
- NAME : DCS
- VERSION : 6
- Customized : false
With the test program developed with the help of Johan Skeet (Error deserializing yaml with yamldotnet - Property not found)
I'm able to process the data, but the serialization does not remain the minus symbol.
The code presently:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
public class UserSoft
{
public string PlatForm { get; set; }
public string Version { get; set; }
public Dictionary<string, Software> SOFTWARE { get; set; }
public class Software
{
public string NAME { get; set; }
public string VERSION { get; set; }
public string Customized { get; set; }
}
}
class Program
{
static void Main(string[] args)
{
string text = File.ReadAllText("test.yaml");
var deserializer = new DeserializerBuilder().Build();
UserSoft deserialized = deserializer.Deserialize<UserSoft>(text);
deserialized.PlatForm = "Linux";
Console.WriteLine("***Dumping Object Using Yaml Serializer***");
var stringBuilder = new StringBuilder();
var serializer = new Serializer();
stringBuilder.AppendLine(serializer.Serialize(deserialized));
//file construction
var stream = new FileStream("Output.yml", FileMode.OpenOrCreate);
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
writer.Write(stringBuilder);
writer.Close();
}
}
}
I think that the symbol is related to List in yaml files. So I have tested the change of the object to:
public class UserSoft
{
public string PlatForm { get; set; }
public string Version { get; set; }
public Dictionary<string, Software> SOFTWARE { get; set; }
public class Software
{
public List<string> NAME { get; set; }
public List<string> VERSION { get; set; }
public List<string> Customized { get; set; }
}
}
No success :(
Exception thrown: 'YamlDotNet.Core.YamlException' in YamlDotNet.dll
An unhandled exception of type 'YamlDotNet.Core.YamlException' occurred in YamlDotNet.dll
(Line: 5, Col: 15, Idx: 75) - (Line: 5, Col: 22, Idx: 82): Exception during deserialization
What I'm doing wrong?
I did not find any discussion that could have the correct help.
It could be nice to keep the original formating, including comments, but I saw in several dicussions that yamldotnet is not able to do that yet. This symbol is a must.
Thank you in advance and sorry for this basic questions.
question from:
https://stackoverflow.com/questions/66065242/c-sharp-usage-of-list-in-yaml-error-in-serialization 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…