SgDotNet
Singapore Professional .NET User Group -For Cool Developers

XmlSerializer and XmlAttributeAttribute

Latest post 12-02-2007 12:29 AM by usoup. 3 replies.
  • 11-15-2007 11:37 AM

    • pp
    • Top 150 Contributor
    • Joined on 05-04-2004
    • Posts 14

    XmlSerializer and XmlAttributeAttribute

    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" />

  • 11-20-2007 2:47 PM In reply to

    • hannes
    • Top 25 Contributor
    • Joined on 05-04-2004
    • South Africa
    • Posts 240

    Re: XmlSerializer and XmlAttributeAttribute

    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.

  • 11-23-2007 11:51 PM In reply to

    Re: XmlSerializer and XmlAttributeAttribute

    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"/>

    - Erik
  • 12-02-2007 12:29 AM In reply to

    • usoup
    • Top 50 Contributor
    • Joined on 12-06-2004
    • Central
    • Posts 79

    Re: XmlSerializer and XmlAttributeAttribute

    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. Smile

    -- Signed with Soup --
Page 1 of 1 (4 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems