2010-01-24 27 views
8

Tôi muốn thêm ở đầu tệp xml của tôi một số ghi chú cho người dùng đọc nó. Tôi không chắc chắn làm thế nào để làm điều này mặc dù với xml serialization.Làm cách nào để chèn các chú thích XML vào tuần tự hóa XML?

Tôi đã nhìn vào bài này

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

nhưng tôi không thực sự chắc chắn những gì đang xảy ra và làm thế nào để thêm video này vào mã của tôi. Về cơ bản tôi chỉ có một số lớp học mà tôi serialize vào xml và dính nó vào một dòng bộ nhớ.

Vì vậy, tôi không chắc chắn về điểm tôi nên thêm nhận xét vào.

Cảm ơn

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

Hiển thị cho chúng tôi mã của bạn :-) – dtb

+0

(Tôi đã thêm giải pháp thay thế cho câu trả lời được liên kết trong câu hỏi.) – dtb

+0

Ok Tôi đã thêm mã của mình. Vì vậy, bạn có thể thấy những gì tôi đang làm và có thể là nơi tôi nên thêm mã đó. – chobo2

Trả lời

17

Chỉ cần đặt một XmlWriter như một trình độ trung cấp giữa MemoryStream và 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; 
} 

Bạn có thể thêm bất kỳ XML nào trước và sau biểu đồ đối tượng được tuần tự hóa (miễn là kết quả là XML hợp lệ).

+5

Tài liệu sẽ không bị thụt lề/định dạng theo mặc định. Vì vậy, bạn cần phải đặt nó trong hàm tạo: XmlWriter.Create (luồng, XmlWriterSettings mới {Indent = true}); Hoặc sử dụng XmlTextWriter: XmlTextWriter writer = XmlTextWriter.Create (stream); writer.Formatting = Formatting.Indented; – row1

Các vấn đề liên quan