LinqTOXML DescendantNodes、DescendantNodesAndSelf和Descendants、DescendantsAndSelf方法

6 篇文章 0 订阅

LinqTOXML DescendantNodes、DescendantNodesAndSelf和Descendants、DescendantsAndSelf方法

Examples:
  1. /*DescendantNodes 原型
  2.          * 
  3.          * 功能:按文档顺序返回此文档或元素的子代节点集合。
  4.          * 
  5.          * public static IEnumerable<XNode> DescendantNodes<T> (
  6.          *   this IEnumerable<T> source
  7.          *   ) where T : XContainer
  8.          * 
  9.          */
  10.         public static void DescendantNodesProtype()
  11.         {
  12.             XDocument xdocument = new XDocument(new XElement("Books",
  13.                 new XElement("Book",
  14.                     new XAttribute("type""Author"),
  15.                     new XElement("FirstName""Joe"),
  16.                     new XElement("LastName""Rattz")),
  17.                new XElement("Book",
  18.                     new XAttribute("type""Editor"),
  19.                     new XElement("FirstName""Ws"),
  20.                     new XElement("LastName""Test"))));
  21.             IEnumerable<XElement> elements = xdocument.Elements("Books");
  22.             foreach (XElement element in elements)
  23.             {
  24.                 Console.WriteLine("Source Element {0}: value={1}", element.Name, element.Value);
  25.             }
  26.             foreach (XNode xNode in elements.DescendantNodes())
  27.             {
  28.                 Console.WriteLine("Nodes {0}", xNode);
  29.             }
  30.         }
  31.         /*DescendantNodesAndSelf 原型
  32.          * 
  33.          * 功能:返回节点的集合,其中包含源集合中的每个元素及其子代节点
  34.          * 
  35.          *public static IEnumerable<XNode> DescendantNodesAndSelf (
  36.          *   this IEnumerable<XElement> source
  37.          *         ) 
  38.          *
  39.          */
  40.         public static void DescendantNodesAndSelf()
  41.         {
  42.             XDocument xdocument = new XDocument(new XElement("Books",
  43.                 new XElement("Book",
  44.                     new XAttribute("type""Author"),
  45.                     new XElement("FirstName""Joe"),
  46.                     new XElement("LastName""Rattz")),
  47.                new XElement("Book",
  48.                     new XAttribute("type""Editor"),
  49.                     new XElement("FirstName""Ws"),
  50.                     new XElement("LastName""Test"))));
  51.             IEnumerable<XElement> elements = xdocument.Elements("Books");
  52.             foreach (XElement element in elements)
  53.             {
  54.                 Console.WriteLine("Source Element {0}: value={1}", element.Name, element.Value);
  55.             }
  56.             foreach (XNode node in elements.DescendantNodesAndSelf())
  57.             {
  58.                 Console.WriteLine("Nodes {0}", node);
  59.             }
  60.         }
  61.         /*Descendants 第一种原型
  62.          * 
  63.          * 功能:返回元素集合,其中包含源集合中每个元素和文档的子代元素
  64.          * 
  65.          * public static IEnumerable<XElement> Descendants<T> (
  66.          *       this IEnumerable<T> source
  67.          *       ) where T : XContainer
  68.          * 
  69.          */
  70.         public static void DescendantsFirstProtype()
  71.         {
  72.             XDocument xdocument = new XDocument(new XElement("Books",
  73.                 new XElement("Book",
  74.                     new XAttribute("type""Author"),
  75.                     new XElement("FirstName""Joe"),
  76.                     new XElement("LastName""Rattz")),
  77.                new XElement("Book",
  78.                     new XAttribute("type""Editor"),
  79.                     new XElement("FirstName""Ws"),
  80.                     new XElement("LastName""Test"))));
  81.             IEnumerable<XElement> elements = xdocument.Elements("Books");
  82.             foreach (XElement element in elements)
  83.             {
  84.                 Console.WriteLine("Source Element {0}: value={1}", element.Name, element.Value);
  85.             }
  86.             foreach (XElement xElement in elements.Descendants())
  87.             {
  88.                 Console.WriteLine("XElements {0}", xElement);
  89.             }
  90.         }
  91.         /*Descendants 第二种原型
  92.          * 
  93.          * 功能:返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。集合中只包括具有匹配 XName 的元素
  94.          * 
  95.          * public static IEnumerable<XElement> Descendants<T> (
  96.          *   this IEnumerable<T> source,
  97.          *   XName name
  98.          *   ) where T : XContainer
  99.          * 
  100.          */
  101.         public static void DescendantsSecondProtype()
  102.         {
  103.             XDocument xdocument = new XDocument(new XElement("Books",
  104.                new XElement("Book",
  105.                    new XAttribute("type""Author"),
  106.                    new XElement("FirstName""Joe"),
  107.                    new XElement("LastName""Rattz")),
  108.               new XElement("Book",
  109.                    new XAttribute("type""Editor"),
  110.                    new XElement("FirstName""Ws"),
  111.                    new XElement("LastName""Test"))));
  112.             IEnumerable<XElement> elements = xdocument.Elements("Books");
  113.             foreach (XElement element in elements)
  114.             {
  115.                 Console.WriteLine("Source Element {0}: value={1}", element.Name, element.Value);
  116.             }
  117.             foreach (XElement xElement in elements.Descendants("FirstName"))
  118.             {
  119.                 Console.WriteLine("XElements {0}", xElement);
  120.             }
  121.         }
  122.         /*DescendantsAndSelf 第一种原型
  123.          * 
  124.          * 功能:返回元素集合,其中包含源集合中的每个元素及其子代元素。
  125.          * 
  126.          *       public static IEnumerable<XElement> DescendantsAndSelf (
  127.          *       this IEnumerable<XElement> source
  128.          *       )
  129.          * 
  130.          */
  131.         public static void DescendantsAndSelf()
  132.         {
  133.             XDocument xdocument = new XDocument(new XElement("Books",
  134.                new XElement("Book",
  135.                    new XAttribute("type""Author"),
  136.                    new XElement("FirstName""Joe"),
  137.                    new XElement("LastName""Rattz")),
  138.               new XElement("Book",
  139.                    new XAttribute("type""Editor"),
  140.                    new XElement("FirstName""Ws"),
  141.                    new XElement("LastName""Test"))));
  142.             IEnumerable<XElement> elements = xdocument.Elements("Books");
  143.             foreach (XElement element in elements)
  144.             {
  145.                 Console.WriteLine("Source Element {0}: value={1}", element.Name, element.Value);
  146.             }
  147.             foreach (XElement xElement in elements.DescendantsAndSelf())
  148.             {
  149.                 Console.WriteLine("XElements {0}", xElement);
  150.             }
  151.         }
  152.         /*DescendantsAndSelf 第二种原型
  153.          * 
  154.          * 功能:返回经过筛选的元素集合,其中包含源集合中的每个元素及其子代元素。集合中只包括具有匹配 XName 的元素。
  155.          * 
  156.          * public static IEnumerable<XElement> DescendantsAndSelf (
  157.          *   this IEnumerable<XElement> source,
  158.          *   XName name
  159.          *   )
  160.          * 
  161.          */
  162.         public static void DescendantsAndSelfSecondProtype()
  163.         {
  164.             XDocument xdocument = new XDocument(new XElement("Books",
  165.                new XElement("Book",
  166.                    new XAttribute("type""Author"),
  167.                    new XElement("FirstName""Joe"),
  168.                    new XElement("LastName""Rattz")),
  169.               new XElement("Book",
  170.                    new XAttribute("type""Editor"),
  171.                    new XElement("FirstName""Ws"),
  172.                    new XElement("LastName""Test"))));
  173.             IEnumerable<XElement> elements = xdocument.Elements("Books");
  174.             foreach (XElement element in elements)
  175.             {
  176.                 Console.WriteLine("Source Element {0}: value={1}", element.Name, element.Value);
  177.             }
  178.             foreach (XElement xElement in elements.DescendantsAndSelf("FirstName"))
  179.             {
  180.                 Console.WriteLine("XElements {0}", xElement);
  181.             }
  182.         }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值