I have an SslStream from which I receive spontaneous XML messages. I want to use XmlTextReader to process XML messages from that stream. Unfortunately it allows me reading only 1st XML. When I call Read after the 1st xml is received, the method throws an exception: Multiple roots in XML documents ("Xml_MultipleRoots"). I believe this is a problem that the stream provides xml messages one by one but the XmlTextReader can handle only one. How to fix this?
First of all, don't use new XmlTextReader() anymore. Use XmlReader.Create(), which has been the preferred way to create an XmlReader since .NET 2.0.
Second, use the overload of Create that accepts an XmlReaderSettings object:
using (var reader = XmlReader.Create(sslStream,
new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Fragment
}))
{
// ... read xml
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With