3

I am using a C# object to Serialize/Deserialize XML.

I would like to add a comment to the XML file whilst serializing, this comment will be a basic <!-- comment --> after the standard xml tag <?xml version="1.0" encoding="UTF-8"?>

This comment does not need to be deserialized, its a basic comment to indicate the product and version that created the xml file.

3
  • and your question is? How do I add this comment in the serialized xml? Commented Jan 18, 2010 at 13:42
  • I am serializing the object to XML, the comment is outside the representation of the object and is just a comment in the xml. Commented Jan 18, 2010 at 13:43
  • I could replace the XMLtag with XMLtag+Comment, but this doesnt seem like the most elegant solution. Commented Jan 18, 2010 at 13:48

3 Answers 3

11

You can serialize directly into a new XDocument using CreateWriter:

XDocument document = new XDocument();
document.Add(new XComment("Product XY Version 1.0.0.0"));
using (XmlWriter writer = document.CreateWriter())
{
    serializer.WriteObject(writer, graph);
}
document.Save(Console.Out);

Alternatively, you can serialize into any other XmlWriter as well:

using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
    writer.WriteStartDocument();
    writer.WriteComment("Product XY Version 1.0.0.0");
    serializer.WriteObject(writer, graph);
    writer.WriteEndDocument();
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1000000 knew something Jon Skeet didn't :P (and it is a good answer)
2

Serialize it to XML, load that XML as an XDocument (or whatever API you want), insert the comment, save it out again. Simple, and should work with whatever API you want to use. You can do it all in memory using a MemoryStream as the temporary storage.

There may be a way of serializing directly into a new XDocument/XmlDocument, but I'm not aware of it.

Comments

0

I believe you can implement IXMLSeriablizable on your objects. If I remember correctly, ReadXML(XmlReader reader) and WriteXML(XmlWriter writer) from that interface are called automatically when serializing/de-serializing (CreateSchema, or whatever the third method is, doesn't need to be implemented).

The caveat of implementing it is that you now may need to implement it on all related nested objects. (i.e if you have a Department object that contains User objects and you want the comment on Departments, you will need to implement IXmlSeriablizable on both Department and User). Also, since you are managing the serialization directly, if you add a new property to a class, you will manually need to modify the IXmlSerializable methods.

I typically implement IXmlSerializable on my objects, as I like having direct control over what gets serialized and how.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.