zoukankan      html  css  js  c++  java
  • 读取XML直接转换为类对象

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfMenuItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <MenuItems>
        <name>xxx</name>
        <menuItemList>
          <MenuItem>
            <name>xxx1</name>
            <moduleNameSpace>ControlsLibrary</moduleNameSpace>
            <modulePath>ControlsLibrary.ControlsImp.ExamTest</modulePath>
            <configPath>configExam.xml</configPath>
          </MenuItem>
          <MenuItem>
            <name>xxxxa</name>
            <moduleNameSpace>ControlsLibrary</moduleNameSpace>
            <modulePath>ControlsLibrary.ControlsImp.ExamTest</modulePath>
            <configPath>configExam.xml</configPath>
          </MenuItem>
          <MenuItem>
            <name>xxxxa1</name>
            <moduleNameSpace>ControlsLibrary</moduleNameSpace>
            <modulePath>ControlsLibrary.ControlsImp.ExamTest</modulePath>
            <configPath>configExam.xml</configPath>
          </MenuItem>
        </menuItemList>
      </MenuItems>
    </ArrayOfMenuItems>
    /// <summary>
            /// 所有的节点集合
            /// </summary>
            public static List<MenuItems> menusList { get; set; }
      /// <summary>
        /// 目录树节点类
        /// </summary>
        public class MenuItems
        {
            /// <summary>
            /// 节点名
            /// </summary>
            public string name { get; set; }
            /// <summary>
            /// 包含的选择项内容
            /// </summary>
            public List<MenuItem> menuItemList { get; set; }
        }
     /// <summary>
        /// 目录树内容类
        /// </summary>
        public class MenuItem
        {
            /// <summary>
            /// 菜单名
            /// </summary>
            public string name { get; set; }
            /// <summary>
            /// 所调用的类库命名空间
            /// </summary>
            public string moduleNameSpace { get; set; }
            /// <summary>
            /// 所调用的类库路径
            /// </summary>
            public string modulePath { get; set; }
            /// <summary>
            /// 类库所调用的配置文件路径
            /// </summary>
            public string configPath { get; set; }
        }
    /// <summary>
            /// 从磁盘读取配置文件信息
            /// </summary>
            /// <param name="type"></param>
            /// <param name="txtFilePath">配置文件路径</param>
            /// <returns></returns>
              public static object ReadConfig<T>(T obj, string filePath)
              {
    
                  StreamReader fileStream =new StreamReader( filePath);
                  string  configtxt = fileStream.ReadToEnd();
                  fileStream.Close();
                  using (StringReader reader = new StringReader(configtxt))
                   {
                       return new XmlSerializer(typeof(T)).Deserialize(reader);
                   }
              }


    最后调用

                PsyAdjust.Common.Module.Menu.menusList = PsyAdjust.Common.Module.ObjectConvert.ReadConfig(PsyAdjust.Common.Module.Menu.menusList, "Menue.xml") as List<PsyAdjust.Common.Module.Menu.MenuItems>;

     
  • 相关阅读:
    Python Web框架Django (三)
    谷歌把域名标记为不安全的解决办法
    tkmybatis VS mybatisplus
    jdbc预编译实现方式
    分析mybatis中 #{} 和${}的区别
    实体类id的几种生成方式
    java 获取mac地址
    javafx 表格某一列设置未复选框
    关闭在chrome里使用双指前进后退页面的功能
    调试maven源代码
  • 原文地址:https://www.cnblogs.com/shaomingrui/p/3556716.html
Copyright © 2011-2022 走看看