5 // Copyright 2007 Novell (http://www.novell.com)
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.Runtime.CompilerServices;
32 namespace System.Xml.Linq
34 public static class Extensions
36 public static IEnumerable<XElement> Ancestors<T> (this IEnumerable<T> source) where T : XNode
38 foreach (T item in source)
39 for (XElement n = item.Parent as XElement; n != null; n = n.Parent as XElement)
43 public static IEnumerable<XElement> Ancestors<T> (this IEnumerable<T> source, XName name) where T : XNode
45 foreach (T item in source)
46 for (XElement n = item.Parent as XElement; n != null; n = n.Parent as XElement)
51 public static IEnumerable<XElement> AncestorsAndSelf (this IEnumerable<XElement> source)
53 foreach (XElement item in source)
54 for (XElement n = item as XElement; n != null; n = n.Parent as XElement)
58 public static IEnumerable<XElement> AncestorsAndSelf (this IEnumerable<XElement> source, XName name)
60 foreach (XElement item in source)
61 for (XElement n = item as XElement; n != null; n = n.Parent as XElement)
66 public static IEnumerable <XAttribute> Attributes (this IEnumerable <XElement> source)
68 foreach (XElement item in source)
69 foreach (XAttribute attr in item.Attributes ())
73 public static IEnumerable <XAttribute> Attributes (this IEnumerable <XElement> source, XName name)
75 foreach (XElement item in source)
76 foreach (XAttribute attr in item.Attributes (name))
80 public static IEnumerable<XNode> DescendantNodes<T> (
81 this IEnumerable<T> source) where T : XContainer
83 foreach (XContainer item in source)
84 foreach (XNode n in item.DescendantNodes ())
88 public static IEnumerable<XNode> DescendantNodesAndSelf (
89 this IEnumerable<XElement> source)
91 foreach (XElement item in source)
92 foreach (XNode n in item.DescendantNodesAndSelf ())
96 public static IEnumerable<XElement> Descendants<T> (
97 this IEnumerable<T> source) where T : XContainer
99 foreach (XContainer item in source)
100 foreach (XElement n in item.Descendants ())
104 public static IEnumerable<XElement> Descendants<T> (
105 this IEnumerable<T> source, XName name) where T : XContainer
107 foreach (XContainer item in source)
108 foreach (XElement n in item.Descendants (name))
112 public static IEnumerable<XElement> DescendantsAndSelf (
113 this IEnumerable<XElement> source)
115 foreach (XElement item in source)
116 foreach (XElement n in item.DescendantsAndSelf ())
120 public static IEnumerable<XElement> DescendantsAndSelf (
121 this IEnumerable<XElement> source, XName name)
123 foreach (XElement item in source)
124 foreach (XElement n in item.DescendantsAndSelf (name))
128 public static IEnumerable<XElement> Elements<T> (
129 this IEnumerable<T> source) where T : XContainer
131 foreach (XContainer item in source)
132 foreach (XElement n in item.Elements ())
136 public static IEnumerable<XElement> Elements<T> (
137 this IEnumerable<T> source, XName name) where T : XContainer
139 foreach (XContainer item in source)
140 foreach (XElement n in item.Elements (name))
144 public static IEnumerable<T> InDocumentOrder<T> (
145 this IEnumerable<T> source) where T : XNode
147 List<XNode> list = new List<XNode> ();
148 foreach (XNode n in source)
150 list.Sort (XNode.DocumentOrderComparer);
151 foreach (T n in list)
155 public static IEnumerable<XNode> Nodes<T> (
156 this IEnumerable<T> source) where T : XContainer
158 foreach (XContainer item in source)
159 foreach (XNode n in item.Nodes ())
163 public static void Remove (this IEnumerable<XAttribute> source)
165 foreach (XAttribute item in source)
169 public static void Remove<T> (this IEnumerable<T> source) where T : XNode
171 var l = new List<T> (source);
172 foreach (T item in l)