欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月

刚接触.net时,就看到过一些用到xml serialization的例子,当时没什么感觉。后来看了sdk和一些较大的程序后,逐渐有了点感觉。我个人感觉xml serialization 的最大好处是可以将一个xml文件对象化,比如说xml中的元素、属性等都可以对应到对象、对象的属性。这样我们可以用对象的思想来操作数据,由于目前主流数据库还只是关系型的(oracle也只是部分面向对象),我们在数据层的操作显得于其他层面有点隔。而xml serialization给了我们这样一个用面向对象的思维来操作数据的可能。据个例子来说,比如说我们要做一个制作工作流程的程序,每个流程有n个阶段,每个阶段有n个人来完成。如果用数据库做,就不可避免的涉及到多表关联。这对于这样一个数据量比较小的程序来说是一个浪费。我们来看一下怎么利用xml来做。

///示例xml文件
<?xml version="1.0"?>
<workflowdata xmlns:xsi="http://www.w3.org/1999/xmlschema-instance" xmlns:xsd="http://www.w3.org/1999/xmlschema">
<workflows>
<workflow name="请假流程" id="1">
<stages>
<stage name="主管核准" id="1" ordernum="1">
<users>
<user name="james" isaudit="1"/>
</users>
</stage>
</stages>
</workflow>
</workflows>
</workflowconfigdata>

///示例cs文件
using system;
using system.collections;
using system.io;
using system.text;
using system.web;
using system.xml.serialization;

namespace portal.modules.workflow
{
    public class workflowconfig
    {
        public static workflowdata settings
        {
            get
            {
                httpcontext context = httpcontext.current;
                workflowdata data = (workflowdata) context.cache["workflowconfig"];
                if (data == null)
                {
                    data = loadsettings(workflowconfig.configfilepath);
                    context.cache.insert("workflowconfig", data, new cachedependency(workflowconfig.configfilepath));
                }
            }
        }
        
        public static string configfilepath
        {
            get
            {
                httpcontext context = httpcontext.current;
                return context.server.mappath(context.request.applicationpath + "//" + "workflow//workflow.xml");
            }
        }

        public static workflowdata loadsettings(string filename)
        {
            streamreader reader = file.opentext(filename);
            xmlserializer serializer = new xmlserializer(typeof(workflowdata));
            workflowdata data = (workflowdata)serializer.deserialize(reader);
            reader.close();
            return data;
        }

        public static void persistsettings(workflowdata data)
        {
            httpcontext context = httpcontext.current;
            string configfilepath = context.server.mappath(context.request.applicationpath + "//workflow//workflow.xml");
            streamwriter writer = file.createtext(configfilepath);
            xmlserializer serializer = new xmlserializer(typeof(workflowdata));
            serializer.serialize(writer, data);
            writer.close();
        }
    }
    
    public class workflowdata
    {
        private workflow[]     _workflows;

        [xmlarray]
        public workflow [] workflows
        {
            get
            {
                return _workflows;
            }
            set
            {
                _workflows = value;
            }
        }

        [xmlignore]
        public workflow this[int workflowid]
        {
            get
            {
                foreach(workflow workflow in workflows)
                {
                    if(workflowid == workflow.id)
                    {
                        return workflow;
                    }
                }
                return null;
            }
        }
    }

    public class workflow
    {
        private string        _name;
        private int            _id;
        private stage []    _stages;
        
        [xmlattribute]
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        
        [xmlattribute]
        public int id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        
        [xmlarray]
        public stage [] stages
        {
            get
            {
                return _stages;
            }
            set
            {
                _stages = value;
            }
        }

        [xmlignore]
        public stage this[int stageid]
        {
            get
            {
                foreach(stage stage in stages)
                {
                    if(stageid == stage.id)
                    {
                        return stage;
                    }
                }
                return null;
            }
        }
    }

    public class stage
    {
        private int            _id;
        private string        _name;
        private int            _ordernum;
        private user []        _users;

        [xmlattribute]
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        
        [xmlattribute]
        public int id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        [xmlattribute]
        public int ordernum
        {
            get
            {
                return _ordernum;
            }
            set
            {
                _ordernum = value;
            }
        }

        [xmlarray]
        public user [] users
        {
            get
            {
                return _users;
            }
            set
            {
                _users = value;
            }
        }
    }

    public class user
    {
        private bool        _isaudit;
        private string        _name;

        public bool isaudit
        {
            get
            {
                return _isaudit;
            }
            set
            {
                _isaudit = value;
            }
        }

        public string name
        {
            get
            {
                return _name;    
            }
            set
            {
                _name = value;
            }
        }
    }
}
这样我们可以这样来操作数据
workflow[0].stages[0].user[0] //得到用户
类似的我就不说了。(上面的代码不可以直接使用,由于涉及到公司,我删了许多,只是演示而已)
这只是小弟的愚见,还望各位大虾指正

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)

相关推荐

  • 暂无文章