首页 > 代码库 > 读取xml数据装配到字典中之应用场景

读取xml数据装配到字典中之应用场景

前段时间看到支付宝设置里面有个多语言这个功能,蛮有意思的,就想双休没事的话做个相关的demo玩玩,可是礼拜六被妹子拽出去玩了一天,来大上海有大半年了,基本没有出去玩过,妹子说我是超级宅男,也不带她出去走走,妹子毕业后从大东北来到上海陪我一起打拼,想想蛮对不起她的。周六一天玩的很充实,走了很多路(一个星期的路都在这一天走完了),看了很多风景,回来洗个澡睡得很舒服,第二天也很清醒,其实除了写代码和玩游戏,发现双休出去走走真的挺不错的。附上几张图片,希望大家有时间多出去走走,也许有意外的收获啊!技术分享

 

技术分享

 

星期天在家没事做,早上八点多就起来做点早饭送妹子上班去,回来就开始撸起代码,发现早上起来撸点代码,一天头脑都很清醒。

首先之前写过一个demo读取xml配置文件并装配到dictionary(key,value)中。这次让它在实际场景中发挥点作用,前面提到过支付宝的多语言切换功能,接下来我们就看怎么将他们联系起来并实现—基于Asp.net MVC的用户信息中英文切换功能。

1.先定义一个读取config文件的类库-NickZhao

 1   public class NickZhao
 2     {
 3         public static string GetConfig(string str, bool Flags = true)
 4         {
 5             string value = http://www.mamicode.com/string.Empty;
 6             Dictionary<string, string> dic = new Dictionary<string, string>();
 7 
 8             if (Flags)
 9             {
10                 dic = GetXml("Ch");
11             }
12             else
13             {
14                 dic = GetXml("En");
15             }
16 
17             if (dic.ContainsKey(str))
18             {
19                 foreach (KeyValuePair<string, string> kv in dic)
20                 {
21                     if (kv.Key == str)
22                     {
23                         value =http://www.mamicode.com/ kv.Value;
24                         break;
25                     }
26                 }
27             }
28             else
29             {
30                 value =http://www.mamicode.com/ str;
31             }
32             return value;
33         }
34 
35         public static Dictionary<string, string> GetXml(string str)
36         {
37             string filePath = string.Empty;
38             Dictionary<string, string> dic = new Dictionary<string, string>();
39 
40             filePath = System.Web.HttpContext.Current.Server.MapPath(string.Concat("", "Xml/" + str + "_UserInfo.xml"));
41 
42 
43             if (System.IO.File.Exists(filePath))
44             {
45                 XmlDocument XmlDoc = new XmlDocument();
46                 XmlDoc.Load(filePath);
47                 XmlNodeList nodes = XmlDoc.SelectNodes("roots/data");
48                 DateTime dt1 = DateTime.Now;
49                 foreach (XmlNode node in nodes)
50                 {
51                     string key = node.Attributes["name"].Value.ToString();
52                     string value = http://www.mamicode.com/node.SelectSingleNode("value").InnerText;
53                     dic.Add(key, value);
54                 }
55             }
56             return dic;
57 
58         }
59     }

2.创建一个UserController和一个UserInfo Model

 public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index(bool flags=true)
        {
            UserInfo userInfo = new UserInfo() { UserNo=3123,UserAge=19,UserName="李伟",Gender="",CardID="34242518794100152",Country="中国",Hobby="听歌,打篮球,游泳"};
            if (!flags) 
            {
                ViewData["Flags"] = false;
            }
            return View(userInfo);
        }
      
    }

public class UserInfo
{
public int UserNo { get; set; }


public string UserName { get; set; }


public int UserAge { get; set; }


public string Country { get; set; }


public string Gender { get; set; }


public string Hobby { get; set; }


public string CardID { get; set; }


}

 

3.给UserContoller创建一个对应的view

 1 @model MvcAjaxTest.Models.UserInfo
 2 @{  
 3    
 4     bool Flags = true;
 5     if (@ViewData["Flags"] != null)
 6     { 
 7      Flags=(bool)@ViewData["Flags"];
 8     }
 9 }
10 <link rel="stylesheet" type="text/css" href="~/Content/css/bootstrap.css" />
11 <script src="~/Scripts/jquery-1.8.2.js"></script>
12 <script type="text/javascript">
13     $(function () {
14         if ("@Flags"=="True") {
15             $("#language").attr("value", "Chinese");
16         }
17         else {
18             $("#language").attr("value", "English");
19         }
20     });
21     
22     function Change() {
23         
24         var selectedLanguage = $("#language option:selected").val();
25         if (selectedLanguage == "English") {
26 
27             window.location.href = "@Url.Action("Index", "User")?Flags=" + false;
28         }
29         else
30         {
31             window.location.href = "@Url.Action("Index", "User")?Flags=" + true;
32         }
33     }
34 </script>
35     
36     <div style="margin-left:820px">
37         <label for="name">@MvcAjaxTest.NickZhao.GetConfig("Language", Flags)</label>
38         <select class="form-control" id="language" onchange="Change()" style="width:110px">
39             <option value="Chinese">中文</option>
40             <option value="English">English</option>
41         </select>
42     </div>
43 <div id="part">
44     <input type="text" id="Test"/>
45     <div class="form-control">
46         <label>@MvcAjaxTest.NickZhao.GetConfig("UserNo", Flags)</label>&nbsp;&nbsp;<span>@Model.UserNo</span>
47         <label>@MvcAjaxTest.NickZhao.GetConfig("UserName", Flags)</label>&nbsp;&nbsp;<span>@Model.UserName</span>
48     </div>
49     <div class="form-control">
50         <label>@MvcAjaxTest.NickZhao.GetConfig("UserAge", Flags)</label>&nbsp;&nbsp;<span>@Model.UserAge</span>
51         <label>@MvcAjaxTest.NickZhao.GetConfig("Hobby", Flags)</label>&nbsp;&nbsp;<span>@Model.Hobby</span>
52     </div>
53     <div class="form-control">
54         <label>@MvcAjaxTest.NickZhao.GetConfig("Country", Flags)</label>&nbsp;&nbsp;<span>@Model.Country</span>
55         <label>@MvcAjaxTest.NickZhao.GetConfig("CardID", Flags)</label>&nbsp;&nbsp;<span>@Model.CardID</span>
56     </div>
57     <div class="form-control">
58         <label>@MvcAjaxTest.NickZhao.GetConfig("Gender", Flags)</label>&nbsp;&nbsp;<span>@Model.Gender</span>
59     </div>
60 </div>   

4.效果:技术分享

技术分享

做得有点简陋。望不吝赐教!

读取xml数据装配到字典中之应用场景