擷取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
如果您覺得本文對你有用,不妨幫忙點個贊,或者在評論裡給我一句贊美,小小成就都是今後繼續為大家編寫優質文章的動力!
歡迎您持續關注我的部落格:)
作者:Ken Wang
出處:http://www.cnblogs.com/Wolfmanlq/
版權所有,歡迎保留原文連結進行轉載:)