views:

368

answers:

1

Hi

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

http://stackoverflow.com/questions/2086326/c-xml-insert-comment-into-xml-after-xml-tag

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

but I am not really sure what is going on and how to add this to my code. Basically I just have some classes that I serialize into xml and stick it in a memory stream.

So I am not sure at what point I should add the comments into.

Thanks

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [XmlRoot("Course")]
    public class MyWrapper 
    {
        public MyWrapper()
        {
            TaskList = new List<Tasks>();
        }

        [XmlElement("courseName")]
        public string CourseName { get; set; }

        [XmlElement("backgroundColor")]
        public string BackgroundColor { get; set; }

        [XmlElement("fontColor")]
        public string  FontColor { get; set; }

        [XmlElement("sharingKey")]
        public Guid SharingKey { get; set; }

        [XmlElement("task")]
        public List<Tasks> TaskList { get; set; }

    }

public class Tasks
{
    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlElement("taskName")]
    public string TaskName { get; set; }

    [XmlElement("description")]
    public string Description { get; set; }

    [XmlElement("taskDueDate")]
    public DateTime TaskDueDate { get; set; }

    [XmlElement("weight")]
    public decimal? Weight { get; set; }

    [XmlElement("beforeDueDateNotification")]
    public int BeforeDueDateNotification { get; set; }

    [XmlElement("outOf")]
    public decimal? OutOf { get; set; }

}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            MyWrapper wrap = new MyWrapper();
            wrap.CourseName = "Comp 1510";
            wrap.FontColor = "#ffffff";
            wrap.BackgroundColor = "#ffffff";
            wrap.SharingKey = Guid.NewGuid();

            Tasks task = new Tasks()
            {
                TaskName = "First Task",
                Type = "Assignment",
                TaskDueDate = DateTime.Now,
                Description = "description",
                BeforeDueDateNotification = 30,
                OutOf = 50.4M
            };

            wrap.TaskList.Add(task);
           var stream = SerializeToXML(wrap);


        }

        static public MemoryStream SerializeToXML(MyWrapper list)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
            MemoryStream stream = new MemoryStream();
            serializer.Serialize(stream, course);
            return stream;  


        }

    }
}
+4  A: 

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).

dtb