首页 > 其他 > 详细

读取XML直接转换为类对象

时间:2014-02-20 08:31:47      阅读:308      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
<?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>\config\Exam.xml</configPath>
      </MenuItem>
      <MenuItem>
        <name>xxxxa</name>
        <moduleNameSpace>ControlsLibrary</moduleNameSpace>
        <modulePath>ControlsLibrary.ControlsImp.ExamTest</modulePath>
        <configPath>\config\Exam.xml</configPath>
      </MenuItem>
      <MenuItem>
        <name>xxxxa1</name>
        <moduleNameSpace>ControlsLibrary</moduleNameSpace>
        <modulePath>ControlsLibrary.ControlsImp.ExamTest</modulePath>
        <configPath>\config\Exam.xml</configPath>
      </MenuItem>
    </menuItemList>
  </MenuItems>
</ArrayOfMenuItems>
bubuko.com,布布扣

 

/// <summary>
        /// 所有的节点集合
        /// </summary>
        public static List<MenuItems> menusList { get; set; }
bubuko.com,布布扣
  /// <summary>
    /// 目录树节点类
    /// </summary>
    public class MenuItems
    {
        /// <summary>
        /// 节点名
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 包含的选择项内容
        /// </summary>
        public List<MenuItem> menuItemList { get; set; }
    }
bubuko.com,布布扣
bubuko.com,布布扣
 /// <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; }
    }
bubuko.com,布布扣

 

bubuko.com,布布扣
/// <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);
               }
          }
bubuko.com,布布扣


最后调用

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

 

 

1
<br> 

读取XML直接转换为类对象

原文:http://www.cnblogs.com/shaomingrui/p/3556716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!