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; }
}
}
http://zhidao.baidu.com/question/70898351.html
参考以上页面
XmlComment 表示XML的注释内容。
你应该是考试吧,实际工作中没有人要求你必须在 XML 中写注释的。
//属性