How to insert XML comments in XML Serialization?

后端 未结 1 1073
野的像风
野的像风 2020-12-05 21:12

I want to add at the top of my xml file some notes for the user who reads it. I am not sure how to do this though with xml serialization.

I was looking at this post<

相关标签:
1条回答
  • 2020-12-05 21:51

    Just put an XmlWriter as an intermediate level between the MemoryStream and the XmlSerializer:

    static public MemoryStream SerializeToXML(MyWrapper list)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
        MemoryStream stream = new MemoryStream();
        XmlWriter writer = XmlWriter.Create(stream);
        writer.WriteStartDocument();
        writer.WriteComment("Product XY Version 1.0.0.0");
        serializer.Serialize(writer, course);
        writer.WriteEndDocument();
        writer.Flush();
        return stream;
    }
    

    Your can add any XML before and after the serialized object graph (as long as the result is valid XML).

    0 讨论(0)
提交回复
热议问题