How can I get XmlSerializer to encode bools as yes/no?

后端 未结 7 2152
情话喂你
情话喂你 2020-12-31 05:49

I\'m sending xml to another program, which expects boolean flags as \"yes\" or \"no\", rather than \"true\" or \"false\".

I have a class defined like:



        
7条回答
  •  庸人自扰
    2020-12-31 06:02

    Making a bool value serialize as "yes" or "no" changes the data type from being a boolean at all. Instead, can you add a separate property which evaluates a boolean and returns "yes" or "no" as appropriate for it's data type? Maybe you could even force "yes" or "no" by making the return type be an enum which only specifies those values.

    public YesOrNo DoYouLoveIt
    {
        get { return boolToEvaluate ? YesOrNo.Yes : YesOrNo.No; }
    }
    

    That might be overkill, but might answer your need. The only reason I bring up an enum for such a simple value is you'd be restricting the values vs. allowing any string.

提交回复
热议问题