aaubry / YamlDotNet

YamlDotNet is a .NET library for YAML
MIT License
2.56k stars 482 forks source link

Yaml format after .Save() #339

Open YvesR opened 6 years ago

YvesR commented 6 years ago

Hello, I am new to YamlDotNet, as everyone uses it, I downloaded and used it from nuget.org.

I use the component for loading, modifying and save key values from yml configuration files.

Example:

---
name: latest
alias: ''
url: http://loki
path: c:\informer\company\latest
files: c:\informer\data\latest
auth: 1
environment: production

When I load, modify and save the new yml file looks like this:

name: latest
alias: ''
url: http://loki
path: c:\informer\company\latest
files: c:\informer\data\latest
auth: &1344666770 1
environment: production
...

So it removed the --- from start, added ... at the end and added a &number at auth: key.

This is my code for load and modify data:

    /// <summary>
    /// Update company settings in krcompprops table from the current company
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void UpdateCompanySettings(string key, string value)
    {
      var reader = new StreamReader(GetCompanyRoot() + companyYml);
      var yaml = new YamlStream();
      yaml.Load(reader);
      reader.Close();
      var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
      ((YamlScalarNode)mapping.Children[key]).Value = value;
      using (TextWriter writer = File.CreateText(GetCompanyRoot() + companyYml)) { 
        yaml.Save(writer);
      }
    }

    /// <summary>
    /// Returns company settings in krcompprops table from the current company
    /// </summary>
    /// <param name="key"></param>
    public dynamic GetCompanySettings(string key)
    {
      using ( var reader = new StreamReader(GetCompanyRoot() + @"\ror\config\company.yml")) { 
        var yaml = new YamlStream();
        yaml.Load(reader);
        var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
        return ((YamlScalarNode) mapping.Children[key]).Value;
      }
    }

So this new resulting yml file seems still be correct, but it confuses me, is everything correct in this case?

seanfinniss commented 6 years ago

// in: using (TextWriter writer = File.CreateText(GetCompanyRoot() + companyYml)) { yaml.Save(writer); }

// change this to: using (TextWriter writer = File.CreateText(GetCompanyRoot() + companyYml)) { yaml.Save(writer, false); // will now suppress the creation of anchors in your nodes. }

aaubry commented 5 years ago

I'm sorry I was unable to answer this question in a timely fashion. As you have certainly moved on to other things, I will close this issue, but feel free to reopen it if this is still an issue.

YvesR commented 5 years ago

Oh somehow I missed the previous answer. I will try it today and if it works I be happy, else I will be back here :)

YvesR commented 5 years ago

Hello,

@seanfinniss answer helped that &prefix before the number is gone, but I still have --- removed and ... added at the end of the yml file.

@aaubry any more advise here?

Thanks, Yves

aaubry commented 5 years ago

I need to look into this. Maybe there is some information that is not being preserved.

cveld commented 3 years ago

Any progress the past year? In my case the yaml stream emits an undesirable explicit document marker as well.

Code:

var mappingNode = new YamlMappingNode();
var yaml = new YamlDocument(mappingNode);            
mappingNode.Add("one", "other");
var yamlStream = new YamlStream(yaml);
var buffer = new StringBuilder();
using (var writer = new StringWriter(buffer))
{
    yamlStream.Save(writer, assignAnchors: false);
    output.WriteLine(writer.ToString());
}

Current output:

one: other
...

Desired output:

one: other
queil commented 2 years ago

@cveld Try doing serialize instead. The below is F# code but it is similar to C#:

  let serializer = SerializerBuilder().Build();

  let SaveFile (path:string) (yaml:YamlStream) =
    use file = File.Open(path, FileMode.Create)
    use writer = new StreamWriter(file, System.Text.Encoding.UTF8)
    serializer.Serialize(writer, yaml.Documents.[0].RootNode)