@@ -27,163 +27,109 @@ The article [How to perform streaming transform of large XML documents](perform-
2727This example creates a custom axis method. You can query it by using a LINQ query. The custom axis method ` StreamRootChildDoc ` can read a document that has a repeating ` Child ` element.
2828
2929``` csharp
30+ using System .Xml ;
31+ using System .Xml .Linq ;
32+
3033static IEnumerable < XElement > StreamRootChildDoc (StringReader stringReader )
3134{
32- using (XmlReader reader = XmlReader .Create (stringReader ))
35+ using XmlReader reader = XmlReader .Create (stringReader );
36+
37+ reader .MoveToContent ();
38+
39+ // Parse the file and display each of the nodes.
40+ while (true )
3341 {
34- reader .MoveToContent ();
35- // Parse the file and display each of the nodes.
36- while (reader .Read ())
42+ // If the current node is an element and named "Child"
43+ if (reader .NodeType == XmlNodeType .Element && reader .Name == " Child" )
3744 {
38- switch (reader .NodeType )
39- {
40- case XmlNodeType .Element :
41- if (reader .Name == " Child" ) {
42- XElement el = XElement .ReadFrom (reader ) as XElement ;
43- if (el != null )
44- yield return el ;
45- }
46- break ;
47- }
45+ // Get the current node and advance the reader to the next
46+ if (XNode .ReadFrom (reader ) is XElement el )
47+ yield return el ;
48+
4849 }
50+ else if (! reader .Read ())
51+ break ;
4952 }
5053}
5154
52- static void Main (string [] args )
53- {
54- string markup = @" <Root>
55- <Child Key="" 01"" >
56- <GrandChild>aaa</GrandChild>
57- </Child>
58- <Child Key="" 02"" >
59- <GrandChild>bbb</GrandChild>
60- </Child>
61- <Child Key="" 03"" >
62- <GrandChild>ccc</GrandChild>
63- </Child>
64- </Root>" ;
65-
66- IEnumerable < string > grandChildData =
67- from el in StreamRootChildDoc (new StringReader (markup ))
68- where (int )el .Attribute (" Key" ) > 1
69- select (string )el .Element (" GrandChild" );
70-
71- foreach (string str in grandChildData ) {
72- Console .WriteLine (str );
73- }
74- }
55+ string markup = """
56+ <Root>
57+ <Child Key="01">
58+ <GrandChild>aaa</GrandChild>
59+ </Child>
60+ <Child Key="02">
61+ <GrandChild>bbb</GrandChild>
62+ </Child>
63+ <Child Key="03">
64+ <GrandChild>ccc</GrandChild>
65+ </Child>
66+ </Root>
67+ """ ;
68+
69+ IEnumerable < string > grandChildData =
70+ from el in StreamRootChildDoc (new StringReader (markup ))
71+ where (int )el .Attribute (" Key" ) > 1
72+ select (string )el .Element (" GrandChild" );
73+
74+ foreach (string str in grandChildData )
75+ Console .WriteLine (str );
7576```
7677
7778``` vb
78- Module Module1
79- Sub Main()
80- Dim markup = "<Root>" &
81- " <Child Key=""01"">" &
82- " <GrandChild>aaa</GrandChild>" &
83- " </Child>" &
84- " <Child Key=""02"">" &
85- " <GrandChild>bbb</GrandChild>" &
86- " </Child>" &
87- " <Child Key=""03"">" &
88- " <GrandChild>ccc</GrandChild>" &
89- " </Child>" &
90- "</Root>"
91-
92- Dim grandChildData =
93- From el In New StreamRootChildDoc( New IO.StringReader(markup))
94- Where CInt (el. @Key ) > 1
95- Select el.<GrandChild>.Value
96-
97- For Each s In grandChildData
98- Console.WriteLine(s)
99- Next
100- End Sub
101- End Module
102-
103- Public Class StreamRootChildDoc
104- Implements IEnumerable( Of XElement)
79+ Imports System.Xml
10580
106- Private _stringReader As IO.StringReader
107-
108- Public Sub New ( ByVal stringReader As IO.StringReader)
109- _stringReader = stringReader
110- End Sub
111-
112- Public Function GetEnumerator() As IEnumerator( Of XElement) Implements IEnumerable( Of XElement).GetEnumerator
113- Return New StreamChildEnumerator(_stringReader)
114- End Function
81+ Module Module1
11582
116- Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
117- Return Me .GetEnumerator()
118- End Function
119- End Class
83+ Public Iterator Function StreamRootChildDoc(stringReader As IO.StringReader) As IEnumerable( Of XElement)
84+ Using reader As XmlReader = XmlReader.Create(stringReader)
85+ reader.MoveToContent()
12086
121- Public Class StreamChildEnumerator
122- Implements IEnumerator( Of XElement)
87+ ' Parse the file and display each of the nodes.
88+ While True
12389
124- Private _current As XElement
125- Private _reader As Xml.XmlReader
126- Private _stringReader As IO.StringReader
90+ ' If the current node is an element and named "Child"
91+ If reader.NodeType = XmlNodeType.Element And reader.Name = "Child" Then
12792
128- Public Sub New ( ByVal stringReader As IO.StringReader)
129- _stringReader = stringReader
130- _reader = Xml.XmlReader.Create(_stringReader)
131- _reader.MoveToContent()
132- End Sub
93+ ' Get the current node and advance the reader to the next
94+ Dim el As XElement = TryCast (XNode.ReadFrom(reader), XElement)
13395
134- Public ReadOnly Property Current As XElement Implements IEnumerator( Of XElement).Current
135- Get
136- Return _current
137- End Get
138- End Property
139-
140- Public ReadOnly Property Current1 As Object Implements IEnumerator.Current
141- Get
142- Return Me .Current
143- End Get
144- End Property
145-
146- Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
147- While _reader.Read()
148- Select Case _reader.NodeType
149- Case Xml.XmlNodeType.Element
150- Dim el = TryCast (XElement.ReadFrom(_reader), XElement)
151- If el IsNot Nothing Then
152- _current = el
153- Return True
96+ If (el IsNot Nothing ) Then
97+ Yield el
15498 End If
155- End Select
156- End While
15799
158- Return False
100+ ElseIf Not reader.Read() Then
101+ Exit While
102+ End If
103+
104+ End While
105+ End Using
159106 End Function
160107
161- Public Sub Reset() Implements IEnumerator.Reset
162- _reader = Xml.XmlReader.Create(_stringReader)
163- _reader.MoveToContent()
164- End Sub
108+ Sub Main()
165109
166- # Region "IDisposable Support"
110+ Dim markup = "<Root>
111+ <Child Key = ""01"" >
112+ <GrandChild>aaa</GrandChild>
113+ </Child>
114+ <Child Key = ""02"" >
115+ <GrandChild>bbb</GrandChild>
116+ </Child>
117+ <Child Key = ""03"" >
118+ <GrandChild>ccc</GrandChild>
119+ </Child>
120+ </Root> "
167121
168- Private disposedValue As Boolean ' To detect redundant calls
122+ Dim grandChildData =
123+ From el In StreamRootChildDoc( New IO.StringReader(markup))
124+ Where CInt (el. @Key ) > 1
125+ Select el.<GrandChild>.Value
169126
170- ' IDisposable
171- Protected Overridable Sub Dispose( ByVal disposing As Boolean )
172- If Not Me .disposedValue Then
173- If disposing Then
174- _reader.Close()
175- End If
176- End If
177- Me .disposedValue = True
178- End Sub
127+ For Each s In grandChildData
128+ Console.WriteLine(s)
129+ Next
179130
180- Public Sub Dispose() Implements IDisposable.Dispose
181- Dispose( True )
182- GC.SuppressFinalize( Me )
183131 End Sub
184- # End Region
185-
186- End Class
132+ End Module
187133```
188134
189135This example produces the following output:
0 commit comments