如何阻止XPathDocument关闭流?

use*_*702 2 .net xpath stream

调用XPathDocument(Stream)构造函数时,流会自动关闭.如何保持流畅通?

use*_*702 5

Stream,StringTextReader过载使用内部的System.Xml.XmlTextReaderImpl类.这个类设置closeInputtrue.

如果要避免这种情况,则需要使用XmlReader重载.它使用未修改的情况下XmlReaderSettings,对于其中的默认值CloseInputfalse.不要忘记将Position背面设置为后面0.

var reader = XmlReader.Create(stream);
var document = new XPathDocument(reader);
stream.Position = 0;
Run Code Online (Sandbox Code Playgroud)