Groups | Blog | Home
all groups > dotnet xml > april 2004 >

dotnet xml : XML Serialization Bug?


AP
4/28/2004 4:42:36 PM
Hello,

I'm using .NET 1.1. I have a class that has these two members:

public bool FurtherActionsRequired;

[System.Xml.Serialization.XmlIgnoreAttribute()]

public bool FurtherActionsRequiredSpecified;

If I serialize the class as is, neither of the above items get serialized.
If I remove the 2nd item and its ignore attribute, FurtherActionsRequired
gets serialized as it should. Is the serializer doing some kind of string
matching that would cause this behavior? Why is this happening?

Thanks,

Adam

Jiho Han
4/29/2004 9:37:57 AM
The second item is how the serialization implements an integer/boolean type
element/attribute.
If your ElementNameSpecified is false, it doesn't get serialized even if
there is a value for ElementName.
If you set ElementNameSpecified to true, ElementName will be serialized.

I think the reason is that there are no null values for types like integer,
bool, or Guid. So I guess anything that cannot be assigned null would use
the pattern above.

Jiho

[quoted text, click to view]

AP
4/29/2004 2:37:53 PM
That doesn't make sense to me at all. Let me simply the example for you.
Take a class:

public class AClass {
public bool FurtherActionsRequired;
public bool FurtherActionsRequiredSpecified;
}

this class serialized to XML produces the following XML output:

<?xml version="1.0" encoding="utf-8"?>
<AClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FurtherActionsRequiredSpecified>false</FurtherActionsRequiredSpecified>
</AClass>

Why does FurtherActionsRequired not get serialized to XML? Is this a bug?

Adam

[quoted text, click to view]

Jiho Han
4/30/2004 9:28:40 AM
Your example should like this:

public class AClass
{
public bool FurtherActionsRequired;

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool FurtherActionsRequiredSpecified;
}

assuming that the field that you're really interested is
"FurtherActionsRequired"

FurtherActionsRequiredSpecified is how .NET handles non-nullable types. For
reference types, if you set the value to null, then the field will not
serialize at all unless you specify otherwise - I don't know how for the
moment, if someone does, feel free to jump in. However, for value types you
can't specify null as the value - i.e. you can't set an integer variable to
null. So .NET uses the above mechanism to say that whether this value-type
field/property should be serialized or not. It's always "<your value-type
variablename>Specified" with the XmlIgnoreAttribute.

Sorry if the explanation isn't any clearer.

Jiho

[quoted text, click to view]

AddThis Social Bookmark Button