Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have an XML file like following
XML
<sun/>
<mon start="3:00 PM" end="11:00 PM"/>
<tue start="3:00 PM" end="11:00 PM"/>


in Sun tag i have set [XmlElement(ElementName = "sun",IsNullable=false)]
but it still shows the <sun> tag . I want to remove this also Please help me


my code like following

C#
[XmlElement(ElementName = "sun", IsNullable = false)]
        public RestaurantDay Sunday
        {
            get;
            set;
        }



C#
public class RestaurantDay{
        [XmlAttribute(AttributeName = "start")]
        public string Start { get; set; }
        [XmlAttribute(AttributeName = "end")]
        public string End { get; set; }
    }
Posted
Updated 8-Jun-15 20:51pm
v2
Comments
Sinisa Hajnal 9-Jun-15 2:53am    
Show the serialization method please. Thank you.

Please read the documentation[^]. Even if document is nullable it must be "visible".
To be able to "remove" xml element, you have to create xsd file, in which you have to declare this element that way:
XML
<xs:element name="sun" type="xs:string" minoccurs="0" maxoccurs="unbounded" xmlns:xs="#unknown" />

It means that sun element is optional (not required).

See: Controlling XML Serialization Using Attributes[^]
Attributes That Control XML Serialization[^]
<xsd:attribute xmlns:xsd="#unknown"><xsd:attribute> Element[^]
 
Share this answer
 
v2
Use System.ComponentModel.DefaultValueAttribute
like
C#
[DefaultValue(0)] public int sun { get; set; }

or if 'Sun' is a class or customtype add something like this to your serialized class
C#
public bool ShouldSerializeSun() {
   return sun != null;
}



See: https://msdn.microsoft.com/en-US/library/53b8022e%28v=vs.110%29.aspx[^]
 
Share this answer
 
v2

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