Some time ago I wrote a (http://www.cnblogs.com/EddyPeng/archive/2012/07/27/2611835.html) about XML serialization and deserialization, and recently suddenly found a problem, that is, when the XML node value is empty, the serialized XML node is in this form <node/>. But how do we write the <node> </node> format. Let's look at the code first.
Tool and test object
public class XmlTextWriterTest : XmlTextWriter { public XmlTextWriterTest(Stream w, Encoding encoding) : base(w, encoding) { } public XmlTextWriterTest(TextWriter w) : base(w) { } public XmlTextWriterTest(string filename, Encoding encoding) : base(filename, encoding) { } public override void WriteEndElement() { base.WriteFullEndElement(); } } public static class Utility { public static string XMLSerialization(object obj) { string result = string.Empty; if (obj != null) { try { XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(); xmlns.Add(string.Empty, string.Empty); XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { XmlTextWriterTest writer = new XmlTextWriterTest(memoryStream, Encoding.Default); writer.Formatting = Formatting.Indented; xmlSerializer.Serialize(writer, obj, xmlns); writer.Flush(); writer.Close(); result = Encoding.Default.GetString(memoryStream.ToArray()); } } catch (Exception ex) { throw ex; } } return result; } public static T XMLDeSerialization<T>(string xml) { T result = default(T); XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); try { using (StringReader stringReader = new StringReader(xml)) { result = (T)xmlSerializer.Deserialize(stringReader); } } catch (Exception ex) { throw ex; } return result; } } [Serializable] [XmlRoot("school")] public class TestObject { [XmlAttribute("descri")] public string descri = "Eddy's xml serializaion demo"; [XmlElement("English")] public English english; [XmlElement("Chinese")] public Chinese chinese; } public class English { [XmlElement("Teacher")] public string Teacher = string.Empty; [XmlElement("Room")] public string Room = "202"; } public class Chinese { [XmlElement("Teacher")] public string Teacher = "Eddy"; [XmlElement("Room")] public string Room = "203"; [XmlElement("Students")] public Students students; } public class Students { [XmlElement("Student")] public List<Student> student; } public class Student { [XmlAttribute("Name")] public string Name = string.Empty; [XmlAttribute("No")] public string No = string.Empty; [XmlAttribute("Age")] public string Age = "19"; }
Call example
TestObject obj = new TestObject() { chinese = new Chinese() { students = new Students() { student = new List<Student>(){ new Student(), new Student() } } }, english = new English() }; string xml = Utility.XMLSerialization(obj); TestObject obj1 = Utility.XMLDeSerialization<TestObject>(xml);
The key to achieving the goal is the following code. Correct, The writeendelement method is rewritten. It can be seen from the literal meaning that it is used to write the complete ending mark, such as: <node> </node>.
public override void WriteEndElement() { base.WriteFullEndElement(); }
Okay, I went to the movies. Hugo watched the semi-episode and went on.