用XmlSerializer序列化对象时,如果某个字符串属性为String.Empty,则序列化后输出的格式是<Name />这样的,有没有办法控制输出的格式变成<Name></Name>这种。
class Utf8XmlTextWriter : XmlTextWriter { public Utf8XmlTextWriter(Stream stream) : base(stream, Encoding.UTF8) { } public override void WriteEndElement() { base.WriteFullEndElement(); } } var fileStream = new FileStream("1.xml",FileMode.Create); var writer = new Utf8XmlTextWriter (fileStream); var serializer = new XmlSerializer(); seraizer.Serialize(writer,obj); fileStream.Dispose();