c# - Xml serializing dynamic string to boolean -
below instance of simple job scheduler parses xml dynamic strings json:
xml
<navigations> <navigation name="facebook" active ="0" ></navigation> </navigations>
c#
list<navigationdata> nds = new list<navigationdata>(); foreach (object cnav in (ienumerable)c.navigations) { navigationdata nd = new navigationdata(); nd.name = (string)((dynamic)cnav).name; nd.active = xmlconvert.toboolean((string)((dynamic)cnav).active); // 3 nds.add(nd); } transitcontent.navigationdata = jsonconvert.serializeobject(nds);
the above program throws exception @ line 3 as:
failed convert string boolean
xmlconvert.toboolean
not able recognize string
convert.toboolean
an other type conversions possibele in scenario? expected result should be:
json
[ { "name": "facebook", "active": false } ]
well yes, "0" isn't valid value boolean. sounds possibly want like:
list<navigationdata> nds = new list<navigationdata>(); foreach (dynamic cnav in (ienumerable)c.navigations) { navigationdata nd = new navigationdata(); nd.name = cnav.name; nd.active = cnav.active != "0"; nds.add(nd); } transitcontent.navigationdata = jsonconvert.serializeobject(nds);
this assuming cnav
expose properties strings (as execution-time type).
Comments
Post a Comment