zoukankan      html  css  js  c++  java
  • 获取资源文件方法

    获取Message方法:

    public class ResourceHelper { private static ErrorCode ErrorMsg; private static string ResourcePath = ConfigManager.Configs["ResourceParames"].ResourcePath.Value; static ResourceHelper() { ErrorMsg = GetErrorMessage(); } private static ErrorCode GetErrorMessage() { string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/"), ResourcePath); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); using (StringReader sr = new StringReader(xmlDoc.OuterXml)) { XmlSerializer xmldes = new XmlSerializer(typeof(ErrorCode)); ErrorCode errorInfo = xmldes.Deserialize(sr) as ErrorCode; return errorInfo; } } public static Message GetMessage(string errorCode) { var errorInfo = ErrorMsg.ErrorCodeList.FirstOrDefault(p => p.Key == errorCode); if (errorInfo == null) { return new Message() { Code = "001", Msg = "资源未找到!" }; } return errorInfo; } }

    实体类定义:

        
     [XmlRoot("ErrorCode")]
        public class ErrorCode
        {
            [XmlArray("Messages")]
            [XmlArrayItem("Message")]
            public List<Message> ErrorCodeList { get; set; }
        }
    
        public class Message
        {
            [XmlAttribute("key")]
            public string Key { get; set; }
    
            [XmlAttribute("code")]
            public string Code { get; set; }
    
            [XmlAttribute("msg")]
            public string Msg { get; set; }
    
        }
    

      

      配置文件格式:

        

    <?xml version="1.0" encoding="utf-8" ?>
    <ErrorCode culture="zh-CHS">
      <Messages>
        <!--订单-->
        <Message key="OrderNotExists" code="OO0001" msg="订单信息不存在" />
        <Message key="OrderNotExists" code="OO0001" msg="订单信息不存在" />
        <Message key="OrderNotExists" code="OO0001" msg="订单信息不存在" />
        <!--门店-->
    
        <!--车辆-->
    
        <!--门店-->
      </Messages>
    </ErrorCode>
    

     配置文件路径:ConfigFiles/Ford/ErrorResource.xml 

      

  • 相关阅读:
    TC SRM 591 (Div2. Practice only)
    SDL2 简单实现图片缩放移动查看
    Linux下socket编程 address already in use 问题
    POJ 2155 二维树状数组
    OJ开发笔记(1)
    开通博客啦~
    [转]STL transform算法中使用toupper函数
    Monkey and Banana HDU 1069
    Ignatius and the Princess IV HDU 1029
    Dungeon Master POJ 2251
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4847259.html
Copyright © 2011-2022 走看看