Hi guys,
With XmlSerializer, for a XmlAttributeAttribute, is it possible to ignore the attribute if its value is not specified?
Exmaple as below
[Serializable]public class TestItem{ [System.Xml.Serialization.XmlAttributeAttribute("DataName")] public string Name; [System.Xml.Serialization.XmlAttributeAttribute("DataAddress")] public uint Address;}
TestItem tt = new TestItem();tt.Name = "testing";
XmlSerializer serl = new XmlSerializer(tt.GetType(), "");StringWriter sw = new StringWriter();serl.Serialize(sw, obj);string sRet = sw.ToString();
The resulted string is as blow. Is it possible to exclude the DataAddress attribute since its value is not specified?
<?xml version="1.0" encoding="utf-16"?><TestItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DataName="testing" DataAddress="0" />
I don't know too much about this, but for what it is worth - the value is specified since it is an uint (a value type) and the default value is 0. When attributes have a value of "null" they don't serialize, so you could change the data type to string. You could also add both an uint and a string property for the same private variable and serialize one of them only, but I think this is messy.
I'm guessing that you dont want to serialize it because you wanted to do some test whether a value has been provided. If this is the case then you can have another property AddressIsSpecified like
System.Xml.Serialization.XmlAttributeAttribute("AddressIsSpecified")]public bool AddressIsSpecified { get {return this._addrIsSpecified;}}
where in in the setter for your address property you set the value of _addrIsSpecified to true
public bool Address { set { this.address =value; this.addrIsSpecified = true; }
}
So if the Address property was not set, your XML will look something like
<?xml version="1.0" encoding="utf-16"?><TestItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DataName="testing" DataAddress="0" AddressIsSpecified="false"/>
hannes: I don't know too much about this, but for what it is worth - the value is specified since it is an uint (a value type) and the default value is 0. When attributes have a value of "null" they don't serialize, so you could change the data type to string. You could also add both an uint and a string property for the same private variable and serialize one of them only, but I think this is messy.
Rightfully so. Default value of an uint (and other non-string) native data type is not null. int is 0, bool is false, etc. Hence, the serialized xml will not be optional.
But, the other way round is not quite so. When we generate a serializable class out of xml schema using xsd tool, if have an optional element, we'll get something like this (following your sample, assuming "Address" is optional element):
[Serializable]public class TestItem{ [System.Xml.Serialization.XmlAttributeAttribute("DataName")] public string DataName; [System.Xml.Serialization.XmlIgnoreAttribute()] public bool DataAddressSpecified;
[System.Xml.Serialization.XmlAttributeAttribute("DataAddress")] public uint DataAddress;}
Somehow, the property <ElementName>Specified is used internally by the serializer to determine the occurence of the element in the XML file. If it is false, the element DataAddress will not be serialized.
Hope it answers.