用C#语言写一个XML文件中的注释,怎么写?如下图。。图中注释的那句怎么写?帮帮吗啊同胞们!

2025-06-22 12:52:02
推荐回答(3个)
回答1:

    public class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Mail));
            using (FileStream fs = new FileStream(@"C:\test.xml", FileMode.OpenOrCreate))
            {
                using (XmlWriter writer = XmlWriter.Create(fs))
                {
                    writer.WriteComment("This is a letter");

                    ser.Serialize(writer, new Mail() { Date = new DateTime(2006, 1, 1), To = "Li", From = "Zhang", Title = "Hello", Body = "how are you?" });
                }
            }
        }

        [Serializable]
        [XmlRoot(ElementName = "mail")]
        public class Mail
        {
            [XmlAttribute(AttributeName = "date")]
            public DateTime Date { get; set; }
            [XmlElement(ElementName = "to")]
            public string To { get; set; }
            [XmlElement(ElementName = "from")]
            public string From { get; set; }
            [XmlElement(ElementName = "body")]
            public string Body { get; set; }
            [XmlElement(ElementName = "title")]
            public string Title { get; set; }
        }
    }

回答2:

http://zhidao.baidu.com/question/70898351.html

参考以上页面
XmlComment 表示XML的注释内容。

你应该是考试吧,实际工作中没有人要求你必须在 XML 中写注释的。

回答3:

//属性