{"id":10449,"date":"2024-05-31T06:59:43","date_gmt":"2024-05-31T06:59:43","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10449"},"modified":"2024-05-31T06:59:43","modified_gmt":"2024-05-31T06:59:43","slug":"parse-only-part-of-yaml-with-yamldotnet","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/dot-net\/parse-only-part-of-yaml-with-yamldotnet","title":{"rendered":"How Do I Parse Only Part of YAML Using YamlDotNet?"},"content":{"rendered":"<p>You can use the DeserializerBuilder to configure a deserializer that only parses the section you&#8217;re interested in.<\/p>\n<h3>Install YamlDotNet:<\/h3>\n<p>Make sure you have the YamlDotNet package installed. You can install it via NuGet package manager in Visual Studio or using the command line:<br \/>\n<code>dotnet add package YamlDotNet<\/code><\/p>\n<h3>Define the Classes:<\/h3>\n<p>Define the C# classes that represent the structure of the YAML data you want to parse.<\/p>\n<p><strong>example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class ConfigOne\r\n{\r\n    public string Name { get; set; }\r\n    public string Stuff { get; set; }\r\n}\r\n<\/pre>\n<h3>Read and Parse the YAML: <\/h3>\n<p>You will need to read the YAML content, extract the part you are interested in, and then deserialize it using YamlDotNet.<\/p>\n<p><strong>example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">\r\nusing System;\r\nusing YamlDotNet.Serialization;\r\nusing YamlDotNet.Serialization.NamingConventions;\r\n\r\npublic class ConfigOne\r\n{\r\n    public string Name { get; set; }\r\n    public string Stuff { get; set; }\r\n}\r\nclass Program\r\n{\r\n    static void Main()\r\n    {\r\n        var yaml = @\"\r\nconfig_one:\r\n  name: foo\r\n  stuff: value\r\n\r\nconfig_two:\r\n  name: bar\r\n  random: value\";\r\n\r\n        \/\/ Parse the YAML document\r\n        var deserializer = new DeserializerBuilder()\r\n            .WithNamingConvention(CamelCaseNamingConvention.Instance)\r\n            .Build();\r\n\r\n        var yamlObject = deserializer.Deserialize<Dictionary<string, object>>(yaml);\r\n\r\n        \/\/ Extract the part you are interested in\r\n        var configOnePart = yamlObject[\"config_one\"].ToString();\r\n\r\n        \/\/ Deserialize the extracted part into the desired object\r\n        var configOneDeserializer = new DeserializerBuilder()\r\n            .WithNamingConvention(CamelCaseNamingConvention.Instance)\r\n            .Build();\r\n\r\n        var configOne = configOneDeserializer.Deserialize<ConfigOne>(configOnePart);\r\n\r\n        \/\/ Output the result\r\n        Console.WriteLine($\"Name: {configOne.Name}, Stuff: {configOne.Stuff}\");\r\n    }\r\n}\r\n<\/pre>\n<h2>Explanation<\/h2>\n<p><strong>1. Read the YAML content:<\/strong> The YAML content is stored in a string variable yaml.<br \/>\n<strong>2. Deserialize the YAML into a dictionary:<\/strong> The first deserialization step converts the YAML into a dictionary where each top-level key (like config_one and config_two) maps to an object. This uses the Dictionary<string, object> type.<br \/>\n<strong>3. Extract the specific part:<\/strong> After the YAML is deserialized into a dictionary, you can access the part of the YAML you are interested in (in this case, config_one). The value of config_one is still in YAML format and needs to be converted to a string.<br \/>\n<strong>4. Deserialize the specific part:<\/strong> Finally, you use another deserializer to convert the extracted YAML string into an instance of ConfigOne.<\/p>\n<h2>Notes<\/h2>\n<p>The NamingConvention is set to CamelCaseNamingConvention to match the C# property naming style.<\/p>\n<p>The extracted config_one part is converted to a string using .ToString(). Depending on the deserializer, you may need to use Serialize method to get a proper YAML string if direct conversion does not work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the DeserializerBuilder to configure a deserializer that only parses the section you&#8217;re interested in. Install YamlDotNet: Make sure you have the YamlDotNet package installed. You can install it via NuGet package manager in Visual Studio or using the command line: dotnet add package YamlDotNet Define the Classes: Define the C# classes that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10450,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[20],"tags":[],"class_list":["post-10449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dot-net"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10449"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10449"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10449\/revisions"}],"predecessor-version":[{"id":10452,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10449\/revisions\/10452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10450"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}