首页 > 其他 > 详细

linq操作xml

时间:2015-09-29 11:16:42      阅读:145      评论:0      收藏:0      [点我收藏+]

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值。

linq操作xml

原文:http://www.cnblogs.com/ArrowTip/p/linqxml.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!