Click here to Skip to main content
15,881,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I try to serialize a n object to xml file.this object containt a property length

C#
[System.Xml.Serialization.XmlAttributeAttribute()]  
        [System.ComponentModel.DefaultValueAttribute(1D)]  
        public double Length  
        {  
            get  
            {  
                return this.lengthField;  
            }  
            set  
            {  
                this.lengthField = value;  
            }  
        } 


When i give the value 1 to this property it does not displayed. Despite when i tried to affect the value 1.65 or 1.75 it appeared

How can fix this problem to display Length=1 ?

What I have tried:

XML
<Person Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">    
<Person1 Attribute1Person1="NamePerson" Attribute2Person1="LastNamePerson" />    
<Person2 Attribute1Person2="NamePerson" Attribute2Person2="LastNamePerson" />    
<Person3 Attribute1Person3="NamePerson" Attribute2Person3="LastNamePerson" />    
<Person4 Attribute1Person4="NamePerson" Attribute2Person4="LastNamePerson" />    
</Person>    
<Man Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price" length="1.75">    
<Man1 Attribute1Person1="NameMan" Attribute2Person1="LastNameMan" length="1.65" />    
<Man2 Attribute1Person2="NameMan" Attribute2Person2="LastNameMan" />    
<Man3 Attribute1Person3="NameMan" Attribute2Person3="LastNameMan" length="1.55" />    
<Man4 Attribute1Person4="NameMan" Attribute2Person4="LastNameMan"  />    
</Man>    
<Women Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">    
<Women1 Attribute1Person1="NameWomen" Attribute2Person1="LastNameWomen" />    
<Women2 Attribute1Person2="NameWomen" Attribute2Person2="LastNameWomen" />    
<Women3 Attribute1Person3="NameWomen" Attribute2Person3="LastNameWomen" />    
<Women4 Attribute1Person4="NameWomen" Attribute2Person4="LastNameWomen" />    
</Women> 
Posted
Updated 8-Nov-20 22:47pm
Comments
[no name] 8-Nov-20 22:36pm    
Try 1.0

1 solution

Either remove the DefaultValue attribute, or override the default value when you serialize to XML:
XmlAttributes.XmlDefaultValue Property (System.Xml.Serialization) | Microsoft Docs[^]
C#
public XmlSerializer CreateSerializer()
{
    var attributeOverrides = new XmlAttributeOverrides();
    attributeOverrides.Add(typeof(Person), nameof(Person.Length), new XmlAttributes
    {
        XmlDefaultValue = double.NaN,
        XmlAttribute = new XmlAttributeAttribute(),
    });
    
    return new XmlSerializer(typeof(Person), attributeOverrides);
}
NB: When you override the serialization attributes for a property, you need to restate all serialization attributes, which is why you need to provide the XmlAttribute property as well as the XmlDefaultValue property.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900