Xml Serialization vs. “True” and “False”

前端 未结 10 1077
傲寒
傲寒 2021-01-07 20:23

I\'m having an issue with deserializing an XML file with boolean values. The source XML files I\'m deserializing were created from a VB6 app, where all boolean values are c

10条回答
  •  臣服心动
    2021-01-07 20:47

    Don't bother fixing a broken xml system or fighting XmlSerializer, especially for something so trivial. It's not worth it. VB6 isn't coming back anytime soon.

    Instead, get hold of the document before it's deserialized and change the values. If you're worried about changing them outside the tags, then use regular expressions or include the angle brackets in the values.

        xml = xml.Replace("True", "true").Replace("False", "false");
    

    It's not going to win any awards for elegance, but it gets you back to work. Sometimes you just have to blue collar it.

    As for performance, yes, you are reiterating through the string O(n), but since the replacement strings are the same length, it doesn't require any moving string elements around. Moreover, depending on the implementation, there might be a greater overhead in modifying XmlSerializer.

提交回复
热议问题