Skip to content

Commit 6df6adb

Browse files
authored
Fixed incorrect logic in XmlReader's example (#43799)
1 parent 5d01b47 commit 6df6adb

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

docs/standard/linq/stream-xml-fragments-xmlreader.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@ static IEnumerable<XElement> StreamRootChildDoc(StringReader stringReader)
3333
{
3434
reader.MoveToContent();
3535
// Parse the file and display each of the nodes.
36-
while (reader.Read())
36+
while (true)
3737
{
3838
switch (reader.NodeType)
3939
{
4040
case XmlNodeType.Element:
4141
if (reader.Name == "Child") {
42-
XElement el = XElement.ReadFrom(reader) as XElement;
42+
XElement? el = XNode.ReadFrom(reader) as XElement;
4343
if (el != null)
4444
yield return el;
45+
46+
// Should only call reader.Read(), if XNode.ReadFrom() was NOT called,
47+
// because XNode.ReadFrom() advances the reader to the next element by itself.
48+
continue;
4549
}
4650
break;
4751
}
52+
53+
if (!reader.Read())
54+
break;
4855
}
4956
}
5057
}
@@ -144,15 +151,29 @@ Public Class StreamChildEnumerator
144151
End Property
145152

146153
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
147-
While _reader.Read()
154+
155+
_reader.MoveToContent()
156+
157+
While True
148158
Select Case _reader.NodeType
149159
Case Xml.XmlNodeType.Element
150-
Dim el = TryCast(XElement.ReadFrom(_reader), XElement)
151-
If el IsNot Nothing Then
152-
_current = el
153-
Return True
160+
If _reader.Name = "Child" Then
161+
Dim el = TryCast(XNode.ReadFrom(_reader), XElement)
162+
If el IsNot Nothing Then
163+
_current = el
164+
Return True
165+
End If
166+
167+
' Should only call _reader.Read(), if XNode.ReadFrom() was NOT called,
168+
' because XNode.ReadFrom() advances the reader to the next element by itself.
169+
Continue While
154170
End If
155171
End Select
172+
173+
If Not _reader.Read() Then
174+
Exit While
175+
End If
176+
156177
End While
157178

158179
Return False

0 commit comments

Comments
 (0)