XML serialization and deserialization (continued)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.