src/HarmonyHub/StreamParser.cs
/// <summary>
/// Reads the next XML element from the input stream.
/// </summary>
/// <param name="expected">A list of element names, that are expected. If
/// provided, and the read element does not match any of the provided names,
/// an XmlException is thrown.</param>
/// <returns>The XML element read from the stream.</returns>
/// <exception cref="XmlException">The input stream contains invalid XML, or
/// the read element is not an XML node of type XmlElement, or the read element
/// is not a start element, or the read element is not one of the expected
/// elements.</exception>
/// <exception cref="IOException">An unrecoverable stream error condition
/// has been encountered and the server has closed the connection.</exception>
Reads the next XML element from the input stream.
Reads the next XML element from the input stream .
public XmlElement NextElement(params string[] expected)
        {
            _reader.Read();
            if (_reader.NodeType == XmlNodeType.EndElement && _reader.Name == "stream:stream")
                throw new IOException("The server has closed the XML stream.");
            if (_reader.NodeType != XmlNodeType.Element)
                throw new XmlException("Unexpected node: '" + _reader.Name + "' of type " + _reader.NodeType);
            if (!_reader.IsStartElement())
                throw new XmlException("Not a start element: " + _reader.Name);
            using (XmlReader inner = _reader.ReadSubtree())
            {
                inner.Read();
                string xml = inner.ReadOuterXml();
                XmlDocument doc = new XmlDocument();
                using (var sr = new StringReader(xml))
                    using (var xtr = new XmlTextReader(sr))
                        doc.Load(xtr);
                XmlElement elem = (XmlElement)doc.FirstChild;
                if (elem.Name == "stream:error")
                {
                    string condition = elem.FirstChild != null ? elem.FirstChild.Name : "undefined";
                    throw new StreamException("Unrecoverable stream error: " + condition);
                }
                if (expected.Length > 0 && !expected.Contains(elem.Name))
                    throw new XmlException("Unexpected XML element: " + elem.Name);
                return elem;
            }
        }
public XmlElement NextElement ( params string [ ] expected ) { _reader . Read ( ) ; if ( _reader . NodeType == XmlNodeType . EndElement && _reader . Name == " " ) throw new IOException ( " " ) ; if ( _reader . NodeType != XmlNodeType . Element ) throw new XmlException ( " " + _reader . Name + " " + _reader . NodeType ) ; if ( ! _reader . IsStartElement ( ) ) throw new XmlException ( " " + _reader . Name ) ; using ( XmlReader inner = _reader . ReadSubtree ( ) ) { inner . Read ( ) ; string xml = inner . ReadOuterXml ( ) ; XmlDocument doc = new XmlDocument ( ) ; using ( var sr = new StringReader ( xml ) ) using ( var xtr = new XmlTextReader ( sr ) ) doc . Load ( xtr ) ; XmlElement elem = ( XmlElement ) doc . FirstChild ; if ( elem . Name == " " ) { string condition = elem . FirstChild != null ? elem . FirstChild . Name : " " ; throw new StreamException ( " " + condition ) ; } if ( expected . Length > 0 && ! expected . Contains ( elem . Name ) ) throw new XmlException ( " " + elem . Name ) ; return elem ; } }
Reads the next XML element from the input stream.
Reads the next XML element from the input stream .
// Advance reader to next node. // We can't use the ReadOuterXml method of reader directly as it places // the cursor on the next element which may result in a blocking read // on the underlying network stream. // Handle unrecoverable stream errors. // throw new IOException("Unrecoverable stream error: " + condition); // This indicates a disconnection event
The XML element read from the stream.
The XML element read from the stream .
The input stream contains invalid XML, or
the read element is not an XML node of type XmlElement, or the read element
is not a start element, or the read element is not one of the expected
elements.
The input stream contains invalid XML or the read element is not an XML node of type XmlElement or the read element is not a start element or the read element is not one of the expected elements .
An unrecoverable stream error condition
has been encountered and the server has closed the connection.
An unrecoverable stream error condition has been encountered and the server has closed the connection .
expected
A list of element names, that are expected. If
provided, and the read element does not match any of the provided names,
an XmlException is thrown.
A list of element names that are expected . If provided and the read element does not match any of the provided names an XmlException is thrown .