2010-01-24 19 views
8

Voglio aggiungere nella parte superiore del mio file xml alcune note per l'utente che lo legge. Non sono sicuro di come farlo con la serializzazione xml.Come inserire commenti XML nella serializzazione XML?

stavo guardando questo post

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

ma non sono davvero sicuro di quello che sta succedendo e come aggiungere questo al mio codice. Fondamentalmente ho solo alcune classi che serializzo in xml e lo inserisco in un flusso di memoria.

Quindi non sono sicuro a che punto aggiungere i commenti.

Grazie

Codice

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; 


     } 

    } 
} 
+0

Mostraci il tuo codice :-) – dtb

+0

(Ho aggiunto una soluzione alternativa alla risposta collegata nella domanda.) – dtb

+0

Ok ho aggiunto il mio codice. Quindi puoi vedere cosa sto facendo ed eventualmente dove dovrei aggiungere quel codice. – chobo2

risposta

17

Basta mettere un XmlWriter come un livello intermedio tra il MemoryStream e 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; 
} 

È possibile aggiungere qualsiasi XML prima e dopo il grafo degli oggetti serializzati (a condizione che il risultato sia XML valido).

+5

Il documento non sarà rientrato/formattato per impostazione predefinita. Quindi è necessario impostarlo nel costruttore: XmlWriter.Create (stream, new XmlWriterSettings {Indent = true}); Oppure utilizzare XmlTextWriter: XmlTextWriter writer = XmlTextWriter.Create (stream); writer.Formatting = Formatting.Indented; – row1