linq操作xml

XDocument 在使用上会比XmlDocument 要简单,其实想想就是一个寻找子节点的问题:

 1   XDocument doc = XDocument.Parse(xmlfile);
 2   XElement root = doc.Root;
 3   XElement elements = root.Elements("permition").Where(u => u.Attribute("name").Value == permition).FirstOrDefault();
 4   if (elements == null)
 5   {
 6        return false;
 7   }
 8         XElement elem = (from u in elements.Descendants("subPermition")
 9                          where u.Attribute("name").Value == info
10                          select u).FirstOrDefault();
11         if (elem != null)
12         {
13             return true;
14         }
15         return false;

1. 找到父节点,然后通过父节点寻找所有的子节点;

2. 匹配子节点对应的Attribute值。

原文地址:https://www.cnblogs.com/ArrowTip/p/linqxml.html