{"id":792,"date":"2016-09-08T20:55:01","date_gmt":"2016-09-08T20:55:01","guid":{"rendered":"http:\/\/tpodolak.com\/blog\/?p=792"},"modified":"2016-09-11T13:26:30","modified_gmt":"2016-09-11T13:26:30","slug":"cake-data-root-level-invalid","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2016\/09\/08\/cake-data-root-level-invalid\/","title":{"rendered":"Cake &#8211; Data at the root level is invalid"},"content":{"rendered":"<p>Couple of weeks <a href=\"http:\/\/tpodolak.com\/blog\/2016\/07\/10\/debugging-cake-scripts\/\">ago<\/a> I mentioned that in my current project we use <i>Cake<\/i> as a build mechanism. Recently we wanted to add simple functionality to our build script, which would allow us to modify one of the <i>App.config<\/i>&#8216;s properties. According to the documentation it should be a straightforward tasks, as all the heavy lifting can be done via <i>XmlPeek<\/i> and <i>XmlPoke<\/i> aliases.<br \/>\nOur initial script looked as follows<\/p>\n<pre lang=\"csharp\">\r\nTask(\"Update-Version\")\r\n.Does(()=>\r\n{\r\n    var appConfigPath = paths.Directories.Project + \"\/\" + Context.File(\"App.config\");\r\n    var version = GetVersion(appConfigPath);\r\n    Information(\"Current version: {0}\", version);\r\n    XmlPokeString(File(appConfigPath), xPathVersionSelector, (++version).ToString());\r\n    version = GetVersion(appConfigPath);\r\n    Information(\"Updated version: {0}\", version);\r\n});\r\n\r\nprivate int GetVersion(string appConfigPath)\r\n{\r\n    return int.Parse(XmlPeek(appConfigPath, xPathVersionSelector));\r\n}\r\n<\/pre>\n<p>And what was surprising for us, it didn&#8217;t work.<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/DataAtTheRootLevel.png\" rel=\"attachment wp-att-793\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/DataAtTheRootLevel.png\" alt=\"dataattherootlevel\" width=\"800\" class=\"aligncenter size-full wp-image-793\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/DataAtTheRootLevel.png 914w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/DataAtTheRootLevel-150x53.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/DataAtTheRootLevel-300x105.png 300w\" sizes=\"(max-width: 914px) 100vw, 914px\" \/><\/a><br \/>\nWe got a bit weird error <i>&#8220;Data at the root level is invalid. Line 1, position 1&#8221;<\/i>. The exception suggested that there was something wrong with our <i>App.config<\/i> file. However if you look at the picture presented above, you will see that we were able to successfully read the file and retrieve value of <i>Version<\/i> with XmlPeek method. After a bit of digging it turned out that <i>XmlPoke<\/i> and <i>XmlPokeString<\/i> are not able to work with relative paths and we have to use absolute one in order to make it work. Fortunately there is an easy way to do that, namely <i>MakeAbsolute<\/i> method. Out script after modifications looks that way<\/p>\n<pre lang=\"csharp\">\r\nTask(\"Update-Version\")\r\n.Does(()=>\r\n{\r\n    var appConfigPath = paths.Directories.Project + \"\/\" + Context.File(\"App.config\");\r\n    var version = GetVersion(appConfigPath);\r\n    Information(\"Current version: {0}\", version);\r\n    XmlPoke(MakeAbsolute(File(appConfigPath)), xPathVersionSelector, (++version).ToString());\r\n    version = GetVersion(appConfigPath);\r\n    Information(\"Updated version: {0}\", version);\r\n});\r\n\r\nprivate int GetVersion(string appConfigPath)\r\n{\r\n    return int.Parse(XmlPeek(appConfigPath, xPathVersionSelector));\r\n}\r\n<\/pre>\n<p>And now everything works fine<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/after.png\" rel=\"attachment wp-att-794\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/after.png\" alt=\"after\" width=\"800\" class=\"aligncenter size-full wp-image-794\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/after.png 923w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/after-150x68.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2016\/09\/cake-data-at-the-root-level-is-invalid\/after-300x137.png 300w\" sizes=\"(max-width: 923px) 100vw, 923px\" \/><\/a><\/p>\n<p>Source code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/CakeDataAtTheRootLevelIsInvalid\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Couple of weeks ago I mentioned that in my current project we use Cake as a build mechanism. Recently we wanted to add simple functionality to our build script, which would allow us to modify one of the App.config&#8216;s properties. According to the documentation it should be a straightforward tasks, as all the heavy lifting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[301],"tags":[302],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-cake","tag-cake"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=792"}],"version-history":[{"count":6,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions"}],"predecessor-version":[{"id":800,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions\/800"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}