Reputation: 1863
I have the following property in an struct
being returned by a WCF service:
[XmlAttribute]
public string BATCHID;
In cases where the value of BATCHID is the empty string, the serialization excludes the property from the resulting XML as if it was never included in the object to begin with. How can I force the serializer to always include this property, even if it is the empty string?
EDIT: I am using the base XML serializer and not the DataContract serializer so EmitDefaultValues is not an option.
Upvotes: 1
Views: 869
Reputation: 11
I am serializing XML strings from vb.net code in VS2010
If in the class definition I have:
<XmlElement("XML_LogonMechanism", IsNullable:=True)> Public LogonMechanism As String
then the output looks like this:
<XML_LogonMechanism xsi:nil="true" />
If, in the code, I set
myClass.LogonMechanism = ""
Then the output includes:
<XML_LogonMechanism />
Upvotes: -1
Reputation: 15941
Try to use the DataMemberAttribute with EmitDefaultValue = true
:
[XmlAttribute, DataMember(EmitDefaultValue=true)]
public string BATCHID_SCHED;
The EmitDefaultValue
cannot be used with the xml serializer.
I think the only way is to implement the IXmlSerializable
interface and manually serialize/deserialize all the fields/properties of your class:
public class Test:IXmlSerializable
{
public string Prop;
public void WriteXml (XmlWriter writer)
{
writer.WriteAttributeString("Prop", Prop ?? "");
}
public void ReadXml (XmlReader reader)
{
if(reader.HasAttributes)
{
Prop = reader.GetAttribute("Prop");
}
}
public XmlSchema GetSchema()
{
return null;
}
}
Upvotes: 2