读取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>\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>

 

/// <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>;

 

 


 

 

你可能感兴趣的:(读取xml)