【问题标题】:How can I stop XPathDocument from closing the stream?如何阻止 XPathDocument 关闭流?
【发布时间】:2015-02-04 11:42:36
【问题描述】:

当调用XPathDocument(Stream) 构造函数时,流会自动关闭。如何保持流打开?

【问题讨论】:

    标签: .net xpath stream


    【解决方案1】:

    StreamStringTextReader 重载使用内部 System.Xml.XmlTextReaderImpl 类。 This class sets closeInput to true.

    如果你想避免这种情况,你需要使用XmlReader 重载。这使用XmlReaderSettings 的未修改实例,CloseInput 的默认值为false。之后不要忘记将Position 设置回0

    var reader = XmlReader.Create(stream);
    var document = new XPathDocument(reader);
    stream.Position = 0;
    

    【讨论】:

      猜你喜欢
      • 2011-11-05
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多