Csharp

Adding Comments to XML Content When Serializing Csharp XML

A brief technical note outlining the basic approach and applicable steps for Adding Comments to XML Content When Serializing Csharp XML.

Those who want to put a timer on your page for time out can edit the code below.

XmlComment Attribute is created

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class XmlCommentAttribute : Attribute
    {
        public XmlCommentAttribute(string value)
        {
            this.Value = value;
        }

        public string Value { get; set; }
    }

The IXmlSerializable interface must be implemented on the object you will serialize.

public class DataSyncSettings : IXmlSerializable        {

       .
       .
       .

Fill in the content of the WriteXml Method as follows

public void WriteXml(XmlWriter writer)
        {
            var properties = GetType().GetProperties();

            foreach (var propertyInfo in properties)
            {
                if (propertyInfo.IsDefined(typeof(XmlCommentAttribute), false))
                {
                    writer.WriteComment(
                        propertyInfo.GetCustomAttributes(typeof(XmlCommentAttribute), false)
                            .Cast<XmlCommentAttribute>().Single().Value);
                }

                writer.WriteElementString(propertyInfo.Name, propertyInfo.GetValue(this, null).ToString());
            }
        }

You can add the feature you want to comment on using the attribute as follows:

[XmlCommentAttribute("The application version, NOT the file version!")] public String Format { get; set; }

The result will be as follows.

  <!--The application version, NOT the file version!-->
  <Format>Please set format property</Format>