public static XElement LoadStream(XmlReader reader, XName rootName, params XName[] streamNames) { ArgumentNullException.ThrowIfNull(reader); ArgumentNullException.ThrowIfNull(rootName); ArgumentNullException.ThrowIfNull(streamNames); for (int i = 0; i < streamNames.Length; i++) { if (streamNames[i] == null) { throw new ArgumentNullException("streamNames[" + i + "]"); } } if (reader.MoveToContent() != XmlNodeType.Element) { throw new InvalidOperationException(string.Format("The reader should be on a node of type '{0}'.", XmlNodeType.Element)); } XElement source = new XElement(rootName); StreamLoader loader = new StreamLoader(reader, streamNames); XName streamName = streamNames.Length > 0 ? streamNames[0] : null; loader.ReadElementUntil(source, streamName); if (streamName != null) { source.AddAnnotation(loader); } return(source); }
public IEnumerator <XElement> GetEnumerator() { if (_loader._index != _index - 1 || (_index - 1 >= 0 && _loader._iterators[_index - 1] != null && _loader._iterators[_index - 1].source != source)) { yield break; } int depth = _loader._baseDepth + _index + 1; XName name = _loader._streamNames[_index]; XName streamName = _loader._streamNames.Length > _index + 1 ? _loader._streamNames[_index + 1] : null; if (_loader.SkipContentUntil(depth, name)) { _loader._iterators[_index] = this; _loader._index = _index; do { source = new XElement(name); _loader.ReadElementUntil(source, streamName); if (streamName != null) { source.AddAnnotation(_loader); } yield return(source); if (_loader._iterators[_index] != this) { yield break; } if (_loader._index != _index) { for (int i = _index + 1; i <= _loader._index; i++) { _loader._iterators[i] = null; } _loader._index = _index; } } while (_loader.SkipContentUntil(depth, name)); _loader._iterators[_index] = null; _loader._index = _index - 1; } }