1

I'm using XmlSerializer to serialize and de-serialize my xml files.

But is there a way to add a comment. I cant find it.

Something like this is what I have tried.

    [XmlElement("Name")]
    [XmlComment("This is the name property")]
    public String Name
    {
        get { return this._name; }
        set { this._name = value; }

    }

This is the code that serializes the Xml

var serializer = new XmlSerializer(typeof(SomeObject));

using (var writer = new StreamWriter(@"c:\path\to\file.xml"))
{
     serializer.Serialize(writer, scene);
}

There for hoping to get the output of

<SomeObject>
   <!-- This is the name property -->
   <Name>My Name is John</Name>
</SomeObject>

Ive looked everywhere. Its probably not available.

IEnumerable
  • 3,610
  • 14
  • 49
  • 78
  • possible duplicate of [How to insert XML comments in XML Serialization?](http://stackoverflow.com/questions/2129414/how-to-insert-xml-comments-in-xml-serialization) – Sascha Mar 17 '14 at 05:15
  • I guess its similar but Im not using memory stream. I was hoping there would be an annotation like above – IEnumerable Mar 17 '14 at 05:37
  • I know it can be done using an XElement or XDocument object, but that is not the same thing as the XmlSerializer. – Adam Zuckerman Mar 17 '14 at 05:39
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 17 '14 at 05:48

1 Answers1

0

example here:

How to write a comment to an XML file when using the XmlSerializer?

Just implement IXmlSerializable in your class and in WriteXml(XmlWriter writer) check for the XmlCommentAttribute to include it in serialisation.

Community
  • 1
  • 1
gReX
  • 1,060
  • 1
  • 20
  • 35