C# 读取Xml文件中的中文

这是.xml中的内容
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- ArenaPackage - ArenaPanel -->
  <string name="ql5l3sgiojb59-n10_ojb5" mz="txt_des">叕说明</string>
  <!-- ArenaPackage - canjiaBtn -->
  <string name="ql5l3sgipzlco-n3_bl1b" mz="n3">开始游戏</string>
  <!-- ArenaPackage - ChakanpaimingPanel -->
  <string name="ql5l3sgix1kg1e-n20_i6io" mz="txt_title">查看分数</string>
</resources>

 




    public static void FindXmlAndReadyToRead() { try { List<String> fileNames = new List<string>(); String Path = ".\\Xml"; DirectoryInfo folder = new DirectoryInfo(Path); //找到当前路径下,所有包含.xml格式的文件的名字 for (int i = 0; i < folder.GetFiles("*.xml").Count(); ++i) { fileNames.Add(folder.GetFiles("*.xml")[i].Name); } if (fileNames.Count <= 0) { Console.WriteLine("Xml文件夹下无xml文件"); return; } foreach (String fileName in fileNames) { Console.WriteLine(fileName); String filePath = Path + "\\" + fileName; ReadXml(filePath); } //字典排序,通过key.length的大小,从大到小排序 dict = dict.OrderByDescending(o => o.Key.Length).ToDictionary(o => o.Key, p => p.Value); } catch (Exception e) { Console.WriteLine("当前目录无Xml文件夹(需要将.xml文件放在Xml文件夹下):" + e.ToString()); } }

         

     //读取xml中包含中文的,去重,去掉包含叕    
  public static void ReadXml(String Path)

  { XmlDocument document = new XmlDocument(); document.Load(Path); XmlNode xn = document.SelectSingleNode("resources"); XmlNodeList xnl = xn.ChildNodes; List<String> lists = new List<string>(); foreach (XmlNode xnode in xnl) { String line = xnode.InnerText; //不包含叕 int ignore = line.IndexOf("叕"); if (ignore <= -1) { //提取中文 Regex regex = new Regex("[\u4e00-\u9fa5]+"); MatchCollection mc = regex.Matches(line); if (mc.Count > 0 && !dict.ContainsKey(line)) { //将所有包含中文的字符串都放在字典里
               //所有读出去的xml包含中文的内容都在dict的key里,之后可以通过遍历dict的key写入到文本
dict.Add(line, "1"); } } } }

  

posted @ 2019-10-23 17:12  darkif  阅读(1824)  评论(0编辑  收藏  举报