Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlTextReader with SslStream - reading multiple xml from stream

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?

like image 324
cubesoft Avatar asked Jun 18 '26 23:06

cubesoft


1 Answers

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
}
like image 83
John Saunders Avatar answered Jun 20 '26 11:06

John Saunders