程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#應用XML序列化操作菜單的辦法

C#應用XML序列化操作菜單的辦法

編輯:C#入門知識

C#應用XML序列化操作菜單的辦法。本站提示廣大學習愛好者:(C#應用XML序列化操作菜單的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved