//advance the parser in chunks until the given node is complete static void AdvanceUntilClosed(XObject ob, XmlParser parser, IReadonlyTextDocument document) { const int chunk = 200; var el = ob as XElement; while (parser.Position < document.Length) { parser.Parse(document.CreateReader(parser.Position, Math.Min(document.Length - parser.Position, chunk))); if (el?.IsClosed ?? ob.IsEnded || !parser.Nodes.Contains(ob.Parent)) { break; } } }