Search code examples
c#xmlstringreader

Why my StringReader is emptied after new XPathDocument(strReader) ?


I am curious why would my StringReader is emptied after I call something like this :

        XPathDocument xPathDoc;
        StringReader strReader = new StringReader(l_xmlFile.content);
        xPathDoc = new XPathDocument(strReader);

l_xmlFile.content is a string and is properly loaded to xPathDoc but after the last line of this code strReader (namely its _s member) is null and length is 0. Why is it happening ? Nothing about this is mentioned on msdn


Solution

  • This is pretty common behavior in .NET, the XPathDocument object takes ownership of the input object. And calls its Dispose() method after it completed reading it. It is StringReader.Dispose() that resets the internal _s and _length Fields.

    The MSDN documentation for the XPathDocument(TextReader) constructor is pretty clumsy but could be interpreted to say so.