脚本宝典收集整理的这篇文章主要介绍了XML(6)自己写一个xml序列化器,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。上篇已经介绍到了通过序列化器将内容写入到XMl文件中。这里还是用PErson类来写。
<span style="font-family:Microsoft YaHei;font-Size:18px;">person p=new person() {Name = "istari", Age = 22, Email = "1061399756@QQ.COM"};</span>
<span style="font-family:Microsoft YaHei;font-size:18px;">mySerialize(p, typeof(person));</span>
<span style="font-family:Microsoft YaHei;font-size:18px;">PRivate static void MySerialize(object obj, Type type) { //创建一个XDocument对象 XDocument document = new XDocument(); //写入xML文件,把类名作为根节点 string nsStr = type.ToString(); string classname = nsStr.Substring(nsStr.LastIndexOf('.') + 1); //写入根节点 XElement rootElement = new XElement(className); //获取当前类型中的所有的属性 PropertyInfo[] properties = type.GetProperties(); //遍历 foreach (PropertyInfo ITem in properties) { rootElement .SetElementValue (item.Name ,item.GetValue (obj,null)); } document .Add (rootElement ); document .Save (className +".xml"); }</span>
其中用到反射来获取person类中的所有属性。
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <person> <Name>istari</Name> <Age>22</Age> <Email>1061399756@qq.com</Email> </person></span>
以上就是XML(6)自己写一个xml序列化器的内容,更多相关内容请关注PHP中文网(www.php.cn)!
以上是脚本宝典为你收集整理的XML(6)自己写一个xml序列化器全部内容,希望文章能够帮你解决XML(6)自己写一个xml序列化器所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。