In all other YAML libraries I've used one can get a node by simply entering the path to it as a string in a get function, is there a feature like that in YAMLDotNet?
Asked
Active
Viewed 2,426 times
3 Answers
1
I decided to read the yaml file and then just convert it to json, here is the code for that if anyone is interested:
public JObject root = null;
public void ReadLanguage()
{
try
{
// Reads file contents into FileStream
FileStream stream = File.OpenRead(languagesPath + filename + ".yml");
// Converts the FileStream into a YAML Dictionary object
var deserializer = new DeserializerBuilder().Build();
var yamlObject = deserializer.Deserialize(new StreamReader(stream));
// Converts the YAML Dictionary into JSON String
var serializer = new SerializerBuilder()
.JsonCompatible()
.Build();
string jsonString = serializer.Serialize(yamlObject);
root = JObject.Parse(jsonString);
plugin.Info("Successfully loaded language file.");
}
catch (Exception e)
{
if (e is DirectoryNotFoundException)
{
plugin.Error("Language directory not found.");
}
else if (e is UnauthorizedAccessException)
{
plugin.Error("Language file access denied.");
}
else if (e is FileNotFoundException)
{
plugin.Error("'" + filename + ".yml' was not found.");
}
else if (e is JsonReaderException || e is YamlException)
{
plugin.Error("'" + filename + ".yml' formatting error.");
}
plugin.Error("Error reading language file '" + filename + ".yml'. Deactivating plugin...");
plugin.Debug(e.ToString());
plugin.Disable();
}
}
I don't recommend doing this if you can avoid it, I happened to need to use yaml and I'd go insane without string paths in the code.

Karl Essinger
- 113
- 10
0
It doesn't look like it, but there is an open feature request. You can follow that, if you're interested: https://github.com/aaubry/YamlDotNet/issues/333

Malik Atalla
- 193
- 8
-
Yeah, I saw that too but it seemed to have been ignored. I decided to just convert it to a json object instead with a library that supports string paths, – Karl Essinger Aug 18 '18 at 03:42
0
I took a stab at it. This code probably needs a bit of cleanup and more error checking, but it seems to work. The general idea is you pass in a path, such as /someNode/anotherNode
and it'll return the YamlNode at that location:
using System;
using System.Collections.Generic;
using YamlDotNet.RepresentationModel;
namespace YamlTransform
{
public static class Extensions
{
public static YamlNode XPath(this YamlDocument doc, string path)
{
if (!(doc.RootNode is YamlMappingNode mappingNode)) // Cannot search non mapping nodes
{
return null;
}
var sections = new Queue<string>(path.Split('/', StringSplitOptions.RemoveEmptyEntries));
while (sections.Count > 1)
{
string nextSection = sections.Dequeue();
var key = new YamlScalarNode(nextSection);
if (mappingNode == null || !mappingNode.Children.ContainsKey(key))
{
return null; // Path does not exist
}
mappingNode = mappingNode[key] as YamlMappingNode;
}
return mappingNode?[new YamlScalarNode(sections.Dequeue())];
}
}
}

Mike Christensen
- 88,082
- 50
- 208
- 326