问题描述
我有,我收到自发的XML消息的SslStream。我想用XmlTextReader的处理从该流XML消息。不幸的是它可以让我只读取第一个XML。当我打电话读取接收第一XML后,该方法将引发异常:在XML文档的多根(Xml_MultipleRoots)。我相信这是流提供XML消息一个接一个,但XmlTextReader的可以只有一个处理一个问题。如何解决这一问题?
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?
推荐答案
首先,不使用新的XmlTextReader()
了。使用 XmlReader.Create()
,这一直是preferred的方式来创建一个的XmlReader
因为.NET 2.0
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.
二,使用的过载创建
接受一个 XmlReaderSettings
目标:
Second, use the overload of Create
that accepts an XmlReaderSettings
object:
using (var reader = XmlReader.Create(sslStream,
new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Fragment
}))
{
// ... read xml
}
这篇关于XmlTextReader的与SslStream - 读取流多个XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!