471,594 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

XmlTextReader and xml fragment

Hi,

any ideas how to read XML fragment from TextReader? XmlTextReader constructor
accepts only Stream or string as source Do I miss something?

thanks
Yuriy
Nov 12 '05 #1
2 4048
"Yuriy" <y.************@gmail.com> wrote in message news:32**********************@msnews.microsoft.com ...
any ideas how to read XML fragment from TextReader? XmlTextReader constructor accepts only Stream or string as source Do I miss
something?


You've missed the constructor that takes a TextReader. You would use the
XmlTextReader( string, ...) constructor if the XML is in a file or at some URL.
You would wrap a string containing an XML fragment in a StringReader (which
IS-A TextReader) and pass that to the XmlTextReader( TextReader, ...) con-
structor.

Herein really lies your solution. Put the XML fragment into a string, and wrap
it in a System.IO.StringReader. Pass that to the TextReader-accepting con-
structor of XmlTextReader.

There's a caveat to be aware of. If the fragment contains multiple top-level
elements, then you must make XmlTextReader believe it is a document with
a single root element.

The easiest way to do this is to wrap these top-level elements as a group in
a single "make-believe" root element so that it appears to XmlTextReader
as a document, and compensate for this artificial container element when
doing your XML processing.

// . . .
xmlFragmentStr = string.Format("<root>{0}</root>", xmlFragmentStr);
StringReader readerSource = new StringReader( xmlFragmentStr);
XmlTextReader reader = new XmlTextReader( readerSource);
// . . .
Derek Harmon
Nov 12 '05 #2
Hi,

it is not suitable for large XML fragments. I have already implemented a
wrapper that
does what I need, but it is interesting why TextReader is not considered
as a source of
by .NET framework in this case. I allow that Stream choice can be ommited,
because it
is easely wrapped into TextReader.

Anyway thank you for answer
"Yuriy" <y.************@gmail.com> wrote in message
news:32**********************@msnews.microsoft.com ...
any ideas how to read XML fragment from TextReader? XmlTextReader
constructor accepts only Stream or string as source Do I miss
something?

You've missed the constructor that takes a TextReader. You would use
the XmlTextReader( string, ...) constructor if the XML is in a file or
at some URL. You would wrap a string containing an XML fragment in a
StringReader (which IS-A TextReader) and pass that to the
XmlTextReader( TextReader, ...) con- structor.

Herein really lies your solution. Put the XML fragment into a string,
and wrap it in a System.IO.StringReader. Pass that to the
TextReader-accepting con- structor of XmlTextReader.

There's a caveat to be aware of. If the fragment contains multiple
top-level
elements, then you must make XmlTextReader believe it is a document
with
a single root element.
The easiest way to do this is to wrap these top-level elements as a
group in
a single "make-believe" root element so that it appears to
XmlTextReader
as a document, and compensate for this artificial container element
when
doing your XML processing.
// . . .
xmlFragmentStr = string.Format("<root>{0}</root>",
xmlFragmentStr);
StringReader readerSource = new StringReader( xmlFragmentStr);
XmlTextReader reader = new XmlTextReader( readerSource);
// . . .
Derek Harmon


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Meir S. | last post: by
4 posts views Thread by Andy Neilson | last post: by
5 posts views Thread by Geoff Bennett | last post: by
5 posts views Thread by Chris | last post: by
2 posts views Thread by XML reading with XMLTextReader | last post: by
4 posts views Thread by CodeRazor | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.