What is the best way to parse an XML boolean attribute (in .NET)?
Solution 1
I think that XmlConvert has all the methods for converting between common language runtime types and XML types. Especially XmlConvert.ToBoolean
handles exactly the boolean values (valid strings are "1" or "true" for true and "0" or "false" for false).
Solution 2
Using CBool
instead of Boolean.Parse
should do the trick: although you'll have to embed it in a try/catch
block (which wouldn't be required when using Boolean.TryParse
), it will successfully convert most 'sensible' boolean values, including true/false and 0/1.
Edit: as pointed out in a comment, this answer is kinda useless for C# programmers, as CBool
is a VB-ism. It maps to Microsoft.VisualBasic.CompilerServices.Conversions::ToBoolean
, which is not suitable for general consumption. Which makes the XMLConvert class pointed out in the accepted answer an even better alternative.
nelci n
Updated on July 09, 2022Comments
-
nelci n almost 2 years
Git subtree & remote information is missing from .git/config file on workspaces other than the committed workspace.
Other users who pulled the git repo are not able to see the remote repo information in their .git/config file
They are not able to update or modify the subtrees.
I used the following commands to add the subtree
$ git remote add -f github.com/google/cadvisor https://github.com/google/cadvisor.git
$ git merge -s ours --no-commit github.com/google/cadvisor/master
$ git read-tree --prefix=github.com/google/cadvisor -u github.com/google/cadvisor/master
$ git commit -m ""
What is the best way to get it working?
-
James Curran over 15 yearsExcellent.. Exactly what I was looking for (knew I should have spent more time poking through the .Net Library Reference)
-
James Curran over 15 yearsI'm only seeing "CBool" defined for VB6, not for .NET. It's possible that VB.NET is aliasing it to some Microsoft.VisualBasic.* method, but I'm using C#, so I'd need to know that underlying method to call.
-
Owen Ivory about 6 yearsBe wary of values like True and False, which XmlConvert views as invalid boolean values. You cannot simply fix this with a simple ToLower() either because XmlConvert throws an exception for any value it does not recognize. XmlConvert is not a silver bullet, but instead it is an interesting tool.
-
Panos about 6 years@OwenIvory Values like True and False are indeed invalid boolean values according to XML Schema specification: w3.org/TR/xmlschema-2/#boolean. See also the discussion in this question: stackoverflow.com/questions/1308491