在我使用过的所有其他YAML库中,只需通过在get函数中将其路径作为字符串输入即可获得节点,YAMLDotNet是否具有类似的功能?
答案 0 :(得分:0)
它看起来不像,但是有一个开放功能请求。如果您有兴趣,可以按照以下步骤操作:https://github.com/aaubry/YamlDotNet/issues/333
答案 1 :(得分:0)
我决定读取yaml文件,然后将其转换为json,如果有人对此感兴趣,请使用以下代码:
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();
}
}
如果您可以避免的话,我不建议您这样做,因为我碰巧需要使用yaml,而且我会在代码中没有字符串路径的情况下发疯。
答案 2 :(得分:0)
我刺了一下。这段代码可能需要一些清理和更多的错误检查,但它似乎可以工作。通常的想法是,您传递诸如local a = {}
a.foo = 0
a.bar = function()
print(a.foo)
end
local bigTable = {
a = a
}
bigTable.a.bar() -- prints 0
之类的路径,它将在该位置返回YamlNode:
<activity>
...
android:launchMode="singleTask
...
</activity>