2

I have an XML with an optional tag as follows:

<Config>
    <CheckForCompleteTransform>true</CheckForCompleteTransform>
    <!-- more tags -->
</Config>

And the class-definition:

public class config {
    [System.Xml.Serialization.XmlElement("CheckForCompleteTransform")]
    public bool? CheckForCompleteTransform { get; set; }
}

This works if I either set the tag to what I provided within my example-XML above or I omit it completely. But what if I provide the tag as <MyTag/>? If this notation is used I want the serializer to set the corresponding property within my class to true, but I awlays get a

System.FormatException: the string literal '' is not valid for type Boolean

Any ideas on how to achieve this?

2 Answers 2

2

Check this here

u can use [XmlElement("CheckForCompleteTransform", IsNullable=true)] CheckForCompleteTransform property in your class

public class config
    {
        [XmlElement("CheckForCompleteTransform", IsNullable = true)]
        public bool? CheckForCompleteTransform { get; set; }
    }

and add xsi:nil="true" attribute to CheckForCompleteTransform tag like this

<CheckForCompleteTransform xsi:nil="true" />
Sign up to request clarification or add additional context in comments.

1 Comment

This works, although I cannot change the actual XML (only for development-purposes I have control on it:( the actual XML is generated by 3rd party and therefor not modifyable)
2

I haven't verified this myself, but have you tried adding a

[XmlElement(IsNullable = true)]

attribute to the CheckForCompleteTransform ?

EDIT:

Ok, how about together with

DefaultValueAttribute(true);

1 Comment

Still the same exception: "string literal '' is no valid value for type Boolean"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.