TextReader or Stream from XmlReader?

My main question: "How can I get a TextReader or Stream object from an existing XmlReader ". Read on for more:

I have an existing XmlReader.  Let's call it reader1.

            // customContext and customSettings may or may not make use reader1's properties
            XmlParserContext customContext = new XmlParserContext(...);
            XmlReaderSettings customSettings = new XmlReaderSettings(...);

            // This won't work, because reader1 isn't a TextReader, Stream or URI.
            XmlReader reader2 = XmlReader.Create(reader1, customSettings, customContext);

I have to use an overload of XmlReader.Create() that takes both an XmlReaderSettings and an XmlParserContext as arguments, because they cannot be set AFTER the XmlReader has been created. (Actually, Settings can be set post-creation, but not the context, and customContext is important.)

Here's the problem. XmlReader.Create() has _three_ overloads that take both XmlParserContext and XmlReaderSettings as arguments. But none of these overloads takes an existing XmlReader. Here are the overloads:

1. XmlReader.Create (TextReader, XmlReaderSettings, XmlParserContext)
2. XmlReader.Create (Stream, XmlReaderSettings, XmlParserContext)
3. XmlReader.Create (String, XmlReaderSettings, XmlParserContext) // String is the URI

I believe that both (1) and (2) should work no matter what XmlReader reader1 is. What's the easiest way to get a TextReader or Stream object from an existing XmlReader I don't think that there are easier ways to do what I'm trying to do.



Answer this question

TextReader or Stream from XmlReader?

  • mike in nyc

    Anachron,

    Can you post a suggestion on the Microsoft Product Feedback Center

    Regards

    David



  • Patrick in Canada

    It doesn't look like this suggestion will fly.

    At least I don't see any scenario where overload XmlReader.Create(XmlReader, , XmlParserContext) has sense.

    Regarding "main" question TextReader from XmlReader. This is impossible and for good reason: How do you know that XmlReader takes data from TextReader. It can read from XPathNavigator instead (XmlNodeReader).

    It could be possible to expose TextReader property on XmTextReader. But there are several reasons not do this as well. One of them that it's hard to make any promises on where TextReader will be positioned in each state of XmlTextReader. User wouldn’t be able read from this TextReader himself any way. After you expose such property any change in the way XmlTextReader reads the stream would be breaking change... So we glad that we don't have such property.

    I expect these are the reasons why TextReader doesn’t expose Stream properly.



  • vasko

    I'm not sure it entirely makes sense. I suspect the reason you can't use an XmlTextReader when you specify the context is that the previous one will already have applied a context (and settings).

    The only thing I can think of is that you could write your own TextReader which takes an XmlTextReader and pulls nodes off, converting them to strings and then allowing those strings to be read. Effectively, that would be a streaming version of creating a new XmlDocument from the existing XmlTextReader, then creating a StringReader round the string version of the OuterXml property of it.

    I could be missing something though.

    Jon



  • Mensana

    Hi!

    I think you can't do here anything. There is XmlReader.Create() that accepts XmlReader and XmlReaderSettings, but no XmlParserContext which you need.

    Perhabs you should write a suggestion/bug to MS.

    Or perhabs you should reconsider your design - pass stream instead of or with reader1 or may be try to create XmlReader subclass that wraps reader1. Can you tell more details why you need such behavior



  • James Walters

    Jon: Thanks for responding! (By the way, this is about XmlReader, not XmlTextReader )

    You're right. What would be really simple and easy is if I could pull out the contents of an XmlReader and convert it to a StringReader (or an object of any other class that inherits from Stream or TextReader.) I hope that somebody here can point out how to do that easily and efficiently. 


  • Sahikon

    Which feature of XmlParserContext are you trying to change with your new reader Is all you want a new reader (look at here if that's the case or to some extent into the ReadSubTree method (v2 only))

    What exactly are you trying to do we may figure other ways of solving your end problem.

    The reason there's no overload taking a ParserContext is because that's not something you can change after you start parsing.

    --VV [MS]
    vascov@microsoft.com


  • TextReader or Stream from XmlReader?