{"id":4660,"date":"2022-04-18T17:29:12","date_gmt":"2022-04-18T15:29:12","guid":{"rendered":"https:\/\/ekiwi.de\/index.php\/4660\/c-xml-boolean-wert-konvertieren\/"},"modified":"2022-12-10T21:49:01","modified_gmt":"2022-12-10T20:49:01","slug":"c-how-to-convert-xml-to-boolean","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/4660\/c-how-to-convert-xml-to-boolean\/","title":{"rendered":"C# How to convert XML to Boolean"},"content":{"rendered":"<p>If you program using C# and store your data in an XML file, then it also happens very often that you have to store values of the Boolean\/Bool type in the XML file. These can be, for example, the states of any checkboxes that the user has clicked in the program or a <a title=\"XAML\/C#: Get position of user control\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4662\/c-get-position-of-usercontrol\/\">UserControl<\/a>.<\/p>\n<h2>Assign XML string to Boolean<\/h2>\n<p>If you now load such an XML file and want to read the data back into your program, you first get only a <a title=\"C# get number of substrings within a string\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4658\/c-sharp-count-substring-in-string\/\">string variable<\/a>, which you have to convert to Boolean.<\/p>\n<p>Fortunately, the <em>System.XML<\/em> namespace provides a suitable method here, so that you do not have to program the type conversion yourself. For this and other type conversions there is the class XmlConvert, which provides a set of static methods to convert XML <a href=\"https:\/\/ekiwi.de\/en\/index.php\/4664\/c-find-or-replace-first-occurrence-of-character-in-string\/\" title=\"C#: Find or replace first occurrence of character in string\">strings<\/a> back to the correct string.<\/p>\n<p>A Boolean value is stored in the XML file as a string of the form TRUE\/FALSE, True\/False or true\/false. The code for converting can then look like this:<\/p>\n<pre><code><span style=\"color: #008080;\">XmlNode<\/span>? xmlMyBool = nd.SelectSingleNode(\"MyBoolValue\");\r\nif (xmlEditable != <span style=\"color: #0000ff;\">null<\/span>)\r\n{\r\n   <span style=\"color: #0000ff;\">bool<\/span> myBoolean = <span style=\"color: #008080;\">XmlConvert<\/span>.ToBoolean(xmlMyBool.InnerText.ToLower());\r\n}<\/code><\/pre>\n<p>The <em>XMLConvert.ToBoolean<\/em> function understands only lowercase values. However, it may happen that it is capitalized in the XML file. Then the method throws an exception. For this reason you should normalize the passed string to lower case with the method <em>.toLower()<\/em>.<\/p>","protected":false},"excerpt":{"rendered":"<p>If you program using C# and store your data in an XML file, then it also happens very often that<\/p>\n","protected":false},"author":2,"featured_media":3379,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[870],"tags":[872,876,875,887,888],"class_list":["post-4660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-c-en","tag-programming-en","tag-visual-studio-en","tag-xaml-en","tag-xml-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/comments?post=4660"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4660\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media\/3379"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media?parent=4660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=4660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=4660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}