Net PerfectDev

Net PerfectDev

  • 1.6k
  • 146
  • 4.6k

Serialize a property using System.ComponentModel.DefaultValueAttribute

Nov 8 2020 4:05 PM
Hello,
 
I try to serialize a n object to xml file.this object containt a property length
  1. [System.Xml.Serialization.XmlAttributeAttribute()]    
  2. [System.ComponentModel.DefaultValueAttribute(1D)]    
  3. public double Length    
  4. {    
  5.     get    
  6.     {    
  7.         return this.lengthField;    
  8.     }    
  9.     set    
  10.     {    
  11.         this.lengthField = value;    
  12.     }    
  13. }  
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
  1. <Person Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">  
  2. <Person1 Attribute1Person1="NamePerson" Attribute2Person1="LastNamePerson" />  
  3. <Person2 Attribute1Person2="NamePerson" Attribute2Person2="LastNamePerson" />  
  4. <Person3 Attribute1Person3="NamePerson" Attribute2Person3="LastNamePerson" />  
  5. <Person4 Attribute1Person4="NamePerson" Attribute2Person4="LastNamePerson" />  
  6. </Person>  
  7. <Man Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price" length="1.75">  
  8. <Man1 Attribute1Person1="NameMan" Attribute2Person1="LastNameMan" length="1.65" />  
  9. <Man2 Attribute1Person2="NameMan" Attribute2Person2="LastNameMan" />  
  10. <Man3 Attribute1Person3="NameMan" Attribute2Person3="LastNameMan" length="1.55" />  
  11. <Man4 Attribute1Person4="NameMan" Attribute2Person4="LastNameMan" />  
  12. </Man>  
  13. <Women Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">  
  14. <Women1 Attribute1Person1="NameWomen" Attribute2Person1="LastNameWomen" />  
  15. <Women2 Attribute1Person2="NameWomen" Attribute2Person2="LastNameWomen" />  
  16. <Women3 Attribute1Person3="NameWomen" Attribute2Person3="LastNameWomen" />  
  17. <Women4 Attribute1Person4="NameWomen" Attribute2Person4="LastNameWomen" />  
  18. </Women>  
How can fix this problem to display Length=1 ?

Answers (2)