Deserialize XML element presence to bool in C#
Asked Answered
R

2

13

I'm trying to deserialize some XML from a web service into C# POCOs. I've got this working for most of the properties I need, however, I need to set a bool property based on whether an element is present or not, but can't seem to see how to do this?

An example XML snippet:

<someThing test="true">
    <someThingElse>1</someThingElse>
    <target/>
</someThing>

An example C# class:

[Serializable, XmlRoot("someThing")]
public class Something
{
    [XmlAttribute("test")]
    public bool Test { get; set; }

    [XmlElement("someThingElse")]
    public int Else { get; set; }

    /// <summary>
    /// <c>true</c> if target element is present,
    /// otherwise, <c>false</c>.
    /// </summary>   
    [XmlElement("target")]
    public bool Target { get; set; }
}

This is a very simplified example of the actual XML and object hierarchy I'm processing, but demonstrates what I'm trying to achieve.

All the other questions I've read related to deserializing null/empty elements seem to involve using Nullable<T>, which doesn't do what I need.

Does anyone have any ideas?

Reviewer answered 15/5, 2012 at 13:57 Comment(0)
G
15

One way to do it would be to use a different property to get the value of the element, then use the Target property to get whether that element exists. Like so.

[XmlElement("target", IsNullable = true)]
public string TempProperty { get; set; }

[XmlIgnore]
public bool Target
{
    get
    {
        return this.TempProperty != null;
    }
}

As even if an empty element exists, the TempProperty will not be null, so Target will return true if <target /> exists

Grosswardein answered 15/5, 2012 at 14:12 Comment(1)
Thanks, was overthinking it :)Reviewer
S
0

Can you explain why you dont want to use nullable types?
When u define an int (as opposed to int?) property in ur poco, it doesnt really represent the underlying xml, and u will simply get the default values for those variables.
IF u assume you wont get empty/null strings or integers with the value 0 in ur xml, youcan used the method Balthy suggested for each of ur properties, or use the method described here


Generally i think its a better idea to create a schema to describe ur xml, and generate classes based on it, while using nullable types, if you really want your classes represent the underlying data.

Stilbestrol answered 15/5, 2012 at 14:19 Comment(3)
I'm deserializing results from a Google Search Appliance. A element <FI/> is present in the <RES> element if the results are auto-filtered, and not present if they aren't. I need to set a property to indicate if a result set is filtered or not - I don't see how a nullable bool will help in this instance?Reviewer
The nullables that are used in such case are for the property tiself, not the boolean that marks if its empty/null or not. if you dont care for FI's content and only want to check if its there or not, then balthys method will do the job. Nullable properites should be used for more advanced scenarions, when the value which existance u want to check is a number or anything else which will get a default, non null value, in the instance of the class ur using. IF <FI> was a numeric type, say integer, and u'd use int FI in ur class it'd always have the value of 0, which is NOT the same as null.Stilbestrol
Thanks, I understand what Nullable's are for. However, in this context, they would be no better than using any non-value type with the simple null check that Balthy suggests. The <FI/> element never has a value - its simply the presence of the element that represents something. So the value of Nullable<T> would always be default(T) or null.Reviewer

© 2022 - 2024 — McMap. All rights reserved.