如何插入XML序列化XML注释? [英] How to insert XML comments in XML Serialization?

查看:220
本文介绍了如何插入XML序列化XML注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的xml的顶部添加一些文件笔记谁读它的用户。我不知道如何做到这一点虽然与XML序列化。

我在看这个帖子

<一个href=\"http://stackoverflow.com/questions/2086326/c-xml-insert-comment-into-xml-after-xml-tag\">http://stackoverflow.com/questions/2086326/c-xml-insert-comment-into-xml-after-xml-tag

 的XDocument文档=新的XDocument();
document.Add(新XComment(产品XY版本1.0.0.0));
使用(VAR作家= document.CreateWriter())
{
    serializer.WriteObject(作家,图形);
}
document.Save(Console.Out);

但我真的不知道是怎么回事,以及如何将它添加到我的code。基本上,我只是有一些类,我序列化到XML和坚持在内存流。

所以,我不知道在什么时候,我要补充的意见之中。

感谢

code

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Xml.Serialization;命名空间的ConsoleApplication1
{
    [XmlRoot(课程)]
    公共类MyWrapper
    {
        公共MyWrapper()
        {
            Tasklist命令=新的List&LT;任务与GT;();
        }        [XmlElement的(课程名)]
        公共字符串课程名{获得;组; }        [XmlElement的(的backgroundColor)]
        公共字符串BackgroundColor中获得{;组; }        [的XmlElement(FONTCOLOR)]
        公共字符串FONTCOLOR {搞定;组; }        [XmlElement的(sharingKey)]
        公众的Guid SharingKey {搞定;组; }        [的XmlElement(任务)]
        公开名单&LT;任务和GT;任务列表{搞定;组; }    }公共类任务
{
    [XmlAttribute(类型)]
    公共字符串类型{搞定;组; }    [的XmlElement(TASKNAME)]
    公共字符串TASKNAME {搞定;组; }    [的XmlElement(说明)]
    公共字符串描述{搞定;组; }    [XmlElement的(taskDueDate)]
    公众的DateTime TaskDueDate {搞定;组; }    [的XmlElement(权重)]
    公共小数?重量{搞定;组; }    [XmlElement的(beforeDueDateNotification)]
    公众诠释BeforeDueDateNotification {搞定;组; }    [XmlElement的(里面出来)]
    公共小数? {里面出来搞定;组; }}

}

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Xml.Serialization;
使用System.IO;命名空间的ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {            MyWrapper包=新MyWrapper();
            wrap.CourseName =比较1510;
            wrap.FontColor =#FFFFFF;
            wrap.BackgroundColor =#FFFFFF;
            wrap.SharingKey = Guid.NewGuid();            任务任务=新任务()
            {
                TASKNAME =第一任务,
                TYPE =分配,
                TaskDueDate = DateTime.Now,
                描述=描述,
                BeforeDueDateNotification = 30,
                =里面出来50.4M
            };            wrap.TaskList.Add(任务);
           VAR流= SerializeToXML(套);
        }        静态公共的MemoryStream SerializeToXML(MyWrapper名单)
        {            XmlSerializer的序列化=新的XmlSerializer(typeof运算(MyWrapper));
            MemoryStream的流=新的MemoryStream();
            serializer.Serialize(流课程);
            返回流;
        }    }
}


解决方案

只要把一个作为的XmlWriter将MemoryStream和XmlSerializer的之间的中间水平:

 静态公共的MemoryStream SerializeToXML(MyWrapper名单)
{
    XmlSerializer的序列化=新的XmlSerializer(typeof运算(MyWrapper));
    MemoryStream的流=新的MemoryStream();
    作家的XmlWriter = XmlWriter.Create(流);
    writer.WriteStartDocument();
    writer.WriteComment(产品XY版本1.0.0.0);
    serializer.Serialize(作家,当然);
    writer.WriteEndDocument();
    writer.Flush();
    返回流;
}

您可以前后序列化对象图后(只要结果是有效的XML)添加任何XML。

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;  


        }

    }
}

解决方案

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

这篇关于如何插入XML序列化XML注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆