博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML序列化与反序列化(续)
阅读量:5869 次
发布时间:2019-06-19

本文共 4179 字,大约阅读时间需要 13 分钟。

  前段时间写了一个关于XML序列化和反序列化的(),最近突然发现一个问题,就是当XML节点值为空时,序列化后的XML节点会是这种形式的<node />。但是我们如果需要<node></node>这种形式的该如何写呢。先看看代码吧

  工具类和测试对象

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
(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; } public class Student { [XmlAttribute("Name")] public string Name = string.Empty; [XmlAttribute("No")] public string No = string.Empty; [XmlAttribute("Age")] public string Age = "19"; }

  调用示例

TestObject obj = new TestObject()            {                chinese = new Chinese()                {                    students = new Students()                    {                        student = new List
(){ new Student(), new Student() } } }, english = new English() }; string xml = Utility.XMLSerialization(obj); TestObject obj1 = Utility.XMLDeSerialization
(xml);

 

  达到目的的关键就是下面这一部分代码,没错,重写了WriteEndElement方法。从字面意思可以看出,它就是用来写完整的结束标志,如:<node></node>的。  

public override void WriteEndElement()        {            base.WriteFullEndElement();        }

  好了,去看电影了,雨果看了半集,继续。

转载于:https://www.cnblogs.com/EddyPeng/archive/2012/08/30/2664360.html

你可能感兴趣的文章
MySQL函数怎么加锁_MYSQL 函数调用导致自动生成共享锁问题
查看>>
MR1和MR2的工作原理
查看>>
Eclipse中修改代码格式
查看>>
GRUB Legacy
查看>>
关于 error: LINK1123: failure during conversion to COFF: file invalid or corrupt 错误的解决方案...
查看>>
hexo博客解决不蒜子统计无法显示问题
查看>>
python实现链表
查看>>
java查找string1和string2是不是含有相同的字母种类和数量(string1是否是string2的重新组合)...
查看>>
Android TabActivity使用方法
查看>>
Eclipse的 window-->preferences里面没有Android选项
查看>>
《麦田里的守望者》--[美]杰罗姆·大卫·塞林格
查看>>
遇到的那些坑
查看>>
央行下属的上海资信网络金融征信系统(NFCS)签约机构数量突破800家
查看>>
[转] Lazy evaluation
查看>>
常用查找算法总结
查看>>
grep 零宽断言
查看>>
被神话的大数据——从大数据(big data)到深度数据(deep data)思维转变
查看>>
修改校准申请遇到的问题
查看>>
python大佬养成计划----win下对数据库的操作
查看>>
监控软件zabbix之安装
查看>>