473,388 Members | 924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 4145
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Casey Watson | last post by:
Hi :) I'm having some major trouble with an XML Client/Server application that I am writing. I am using NetworkStream with CryptoStream to Read and Write XML between computers. Now, whenever I...
4
by: Meir S. | last post by:
I think the following is a bug in XmlTextReader: I need to process large XMLs, that are typically constructed of many small elements nested in the root element. Each inner element represents a...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
5
by: Geoff Bennett | last post by:
While parsing an XML document, my TextReader instance skips nodes. For example, in this fragment: <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14"> <RelatedEntityRef...
5
by: Chris | last post by:
Hi, the docs say : "The Xml-document is not loaded into memory when using XmlTextReader, as opposed to using the DOM where the entire document is loaded in memory" but, when using...
1
by: SHC | last post by:
Hi all, I did the "Build" on the attached code in my VC++ .NET 2003 - Windows XP Pro PC. On the c:\ screen, I got the following: Microsoft Development Environment An unhandled exception of type...
2
by: XML reading with XMLTextReader | last post by:
im trying to read an xml file which is in the wwwroot folder.im using IIS on XP Prof. my code is...
4
by: CodeRazor | last post by:
I am trying to use an XmlTextReader to retrieve data. I need to use an XmlTextReader because it is faster than using an XmlDocument. I have found an inelegant way of retrieving each item's title...
3
by: apiringmvp | last post by:
All, So I am creating a function that gets a short blurb of html from a blog. I would like to retain all html formating and images. The code below works well, with the exception of one issue....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.