欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用XML序列化操作菜单的方法

程序员文章站 2023-12-15 08:36:22
本文实例讲述了c#使用xml序列化操作菜单的方法。分享给大家供大家参考。具体分析如下: 之前的一篇文章《c#递归读取xml菜单数据的方法》没使用xml序列化来操作菜单,而...

本文实例讲述了c#使用xml序列化操作菜单的方法。分享给大家供大家参考。具体分析如下:

之前的一篇文章《c#递归读取xml菜单数据的方法》没使用xml序列化来操作菜单,而且发现那还有一个问题,就是在xml菜单的某个菜单节点前加上一些注释代码的就不能读取,现在使用xml序列化后可以很方便的读取,故在此写一写。

xml菜单的节点代码如下:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
  <zcsoft.net xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">
   <applications>
      <application id ="oa" text="oa管理系统">
        <modules>
          <module id="oa_system" text="系统管理">
            <menus>
            <menu id="oa_system_usermanager" text="人员管理" url="system/usermanager/usermanagerlist.aspx"> </menu>
              <menu id="oa_system_rolemanager" text="角色管理" url="system/rolemanager/rolemanagerlist.aspx"></menu>
              <menu id="oa_system_loginlog" text="登录日志" url="system/log/loginloglist.aspx"></menu>
            <menu id="oa_system_operatelog" text="操作日志" url="system/log/operateloglist.aspx"></menu>
            </menus>
          </module>
 
          <module id="oa_targetmanage" text="目标管理">
            <menus>
              <menu id="oa_targetmanage_targetsetup" text="目标设定" url="oa/targetmanage/targetsetuplist.aspx">
              </menu>
            </menus>
          </module>
        </modules>
      </application>
    <applications>
</zcsoft.net>

这里面有一个节点:applications(应用程序节点),里面可以放多个application,而每个application节点里面只包含一个modules(模块节点),modules有多个module,每个module又只有一个menus(菜单节点),而menus里有多个menu。而每个节点都有两个公共的属性:id和text。

故这里写一个公共的属性类:baseattribute,前面记得加上序列化标识serializable,代码如下:

复制代码 代码如下:

[serializable]
    public class baseattribute
    {
        [xmlattribute(attributename = "id")]
        public string id { get; set; }

        [xmlattribute(attributename = "text")]
        public string text { get; set; }
    }

每个节点都有两个类,一个是列表,一个是实体,实体类需继承公共的类,如下:

复制代码 代码如下:

[serializable]
    public class applicationlist
    {
        public applicationlist()
        {
            this.applications = new list<application>();
        }
        [xmlelement(elementname = "application")]
        public list<application> applications { get; set; }
    }

[serializable]
    public class application : baseattribute
    {
        public application()
        {
            this.modules = new modulelist();
        }
        [xmlelement(elementname = "modules")]
        public modulelist modules { get; set; }

        [xmlattribute(attributename = "url")]
        public string url { get; set; }
    }


[serializable]
    public class modulelist
    {
        public modulelist()
        {
            this.modules = new list<module>();
        }
        [xmlelement(elementname = "module")]
        public list<module> modules { get; set; }
    }

[serializable]
    public class module : baseattribute
    {
        public module()
        {
            this.display = "true";
            this.menus = new menulist();
        }
        [xmlelement(elementname = "menus")]
        public menulist menus { get; set; }

        [xmlattribute(attributename = "display")]
        public string display { get; set; }

        [xmlattribute(attributename = "url")]
        public string url { get; set; }
    }


[serializable]
    public class menulist
    {
        public menulist()
        {
            this.menus = new list<menu>();
        }
        [xmlelement(elementname = "menu")]
        public list<menu> menus { get; set; }
    }

/// <summary>
    /// 菜单类
    /// </summary>
    [serializable]
    public class menu : baseattribute
    {
        public menu()
        {
            this.securityable = false;
            this.popup = false;
        }

        [xmlattribute(attributename = "popup")]
        public bool popup { get; set; }

        [xmlattribute(attributename = "securityable")]
        public bool securityable { get; set; }

        [xmlattribute(attributename = "url")]
        public string url { get; set; }
    }

下面几个类是用于操作xml的,代码如下:

复制代码 代码如下:

[serializable,xmlroot("zcsoft.net")]
    public class zcsoftplateform
    {
        public zcsoftplateform()
        {
            this.applications = new applicationlist();
        }
        [xmlelement(elementname = "applications")]
        public applicationlist applications { get; set; }
    }

 /// <summary>
    /// 操作xml类
    /// </summary>
    public class loadfoundationxml
    {
        private static zcsoftplateform _foundationobject;
        static loadfoundationxml()
        {
            if (_foundationobject == null)
            {
                string path = appdomain.currentdomain.basedirectory + "foundation.xml";
                if (file.exists(path))
                {
                    _foundationobject = serialization.toobject<zcsoftplateform>(path);
                }
            }
        }
        private loadfoundationxml()
        {
        }

        public static zcsoftplateform plateformobject
        {
            get
            {
                return _foundationobject;
            }
        }
    }

最后就是一个序列化操作类,如下:

复制代码 代码如下:

/// <summary>
    /// 序列化xml类
    /// </summary>
    public class serialization
    {
        public static t toobject<t>(string xmlfile)
        {
            filestream stream = null;
            t local = activator.createinstance<t>();
            try
            {
                xmlserializer serializer = new xmlserializer(typeof(t));
                stream = new filestream(xmlfile, filemode.open, fileaccess.read, fileshare.read);
                local = (t)serializer.deserialize(stream);
                stream.close();
            }
            catch
            {
                while (stream != null)
                {
                    stream.close();
                    break;
                }
                throw new exception("xml deserialization failed!");
            }
            return local;
        }
    }

在后台可以这样调用,这里没用递归,如下

复制代码 代码如下:

private static zcsoftplateform plateform;

list<menutreedata> list = new list<menutreedata>();

plateform = loadfoundationxml.plateformobject;

//使用操作xml类来读取xml
var applist = plateform.applications.applications;
foreach (var application in applist)
{
    var appdata = new menutreedata();
    appdata.itemid = 0;
    appdata.templateid = 0;
    appdata.itemcode = application.id;
    appdata.itemname = application.text;
    appdata.itemtype = "folder";
    appdata.itemorder = 0;
    appdata.visible = true;
    appdata.itemurl = null;
    appdata.parentitem = null;
    appdata.applicationcode = application.id;
    appdata.applicationname = application.text;
    appdata.modulecode = null;
    appdata.modulename = null;
    appdata.securityable = false;
    appdata.popup = false;
    list.add(appdata);

    if (application.modules!=null)
    {
 foreach (var module in application.modules.modules)
 {
     bool display = module.display.tolower() == "true" ? true : false;
     string parentitem = null;//上一节点id
     var moddata = new menutreedata();
     moddata.itemid = 0;
     moddata.templateid = 0;
     moddata.itemcode = module.id;
     moddata.itemname = module.text;
     moddata.itemtype = "folder";
     moddata.itemorder = 0;
     moddata.visible = display;
     moddata.itemurl = null;
     if (display)
     {
  parentitem = application.id;
     }

     moddata.parentitem = parentitem;
     moddata.applicationcode = application.id;
     moddata.applicationname = application.text;
     moddata.modulecode = module.id;
     moddata.modulename = module.text;
     moddata.securityable = false;
     moddata.popup = false;
     list.add(moddata);

     if (module.menus!=null)
     {
  foreach (var menu in module.menus.menus)
  {
      var mdata = new menutreedata();
      mdata.itemid = 0;
      mdata.templateid = 0;
      mdata.itemcode = menu.id;
      mdata.itemname = menu.text;
      mdata.itemtype = "menu";
      mdata.itemorder = 0;
      mdata.visible = true;
      mdata.itemurl = menu.url;
    
      if (display)
      {
   /*
   *  如果该菜单的所属模块中的display属性设置为可见true
   * (注意:没有设置则默认为可见),则菜单的上级为module的id
   */
   mdata.parentitem = module.id;
      }
      else
      {
   /*如果该菜单的所属模块中的display属性设置为不可见false,
    * 则菜单的上级为application的id
    */
   mdata.parentitem = application.id;
      }
      mdata.applicationcode = application.id;
      mdata.applicationname = application.text;
      mdata.modulecode = module.id;
      mdata.modulename = module.text;
      mdata.securityable = false;
      mdata.popup = false;
      list.add(mdata);
  }
     }
 }
    }
}

使用到的菜单实体类:

复制代码 代码如下:

/// <summary>
/// 系统菜单
/// </summary>
public class menutreedata
{
        public int itemid { get; set; }

        public int templateid { get; set; }

        public string itemcode { get; set; }

        public string itemname { get; set; }

        public string itemtype { get; set; }

        public int itemorder { get; set; }

        public bool visible { get; set; }

        public string itemurl { get; set; }

        public string parentitem { get; set; }

        public string applicationcode { get; set; }

        public string applicationname { get; set; }

        public string modulecode { get; set; }

        public string modulename { get; set; }

        public bool securityable { get; set; }

        public bool popup { get; set; }
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: