Support for stream based reading from XmlReader?

N

nickdu

I see XmlReader has a method named ReadValueChunk() which is a chunking
mechanism for reading large text from an XML document. However, that method
is not implemented in XmlTextReader. Is there some way to chunk the reads of
large textual data?
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
J

Jeroen Mostert

nickdu said:
I see XmlReader has a method named ReadValueChunk() which is a chunking
mechanism for reading large text from an XML document. However, that method
is not implemented in XmlTextReader. Is there some way to chunk the reads of
large textual data?

There are no framework classes you can directly instantiate that support
this. You must use XmlReader.Create(); the XmlTextReaderImpl instance you
get this way does support ReadValueChunk().
 
S

Steven Cheng

Hi Nick,

As Jeroen suggested, for createing xml reader/writer, we're recommended to
use the factory method(Create) on XmlReader/XmlWriter abstract class. There
are several benefits on this(such as reusable setting and xmlreader
chaining...), here is the MSDN document mentioned this:

#Creating XML Readers
http://msdn.microsoft.com/en-us/library/9khb6435.aspx


And by default XmlTextReader implementation is used for
XmlReader.Create(when you supply a stream based input). e.g.

===============================
static void RunXmlReader()
{
byte[] bytes = Encoding.UTF8.GetBytes(@"<testChuck>
Master pages in ASP.NET 2.0 applications
are the pages that enable you to provide a consistent look to your web
application. In ASP.NET 1.1, to achieve a consistent look across a website,
you need to use User controls and place them on each page. The master pages
eliminate the need to place the header, footer, or other important sections
on each page of your website repeatedly. The master pages are programmable
and contain methods, properties, and controls that can be made visible in
all other content pages. However, these elements need to be declared with
the Public scope in the master pages. You can build your content pages,
which have a unique content of their own and then merge the master page
with it to provide the page a consistent look. A single website can have
more than one master page. To use master page in your content page, you
need to reference the master page that you want to use. This article
discusses the way that you can use to reference a master page in your
content page.
</testChuck>"
);

MemoryStream ms = new MemoryStream(bytes);

XmlReaderSettings settings = new XmlReaderSettings();
XmlReader xtr = XmlReader.Create(ms);
Console.WriteLine(xtr.GetType());


xtr.ReadToFollowing("testChuck");
xtr.Read();

int cnt = 0;
char[] cbuf = new char[128];

cnt = xtr.ReadValueChunk(cbuf, 0, cbuf.Length);

while (cnt > 0)
{
Console.Write(new string(cbuf,0, cnt));
cnt = xtr.ReadValueChunk(cbuf, 0, cbuf.Length);
}

xtr.Close();
}
===============================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
N

nickdu

Thanks. I'm curious, why is it implemented this way? Why not just put the
implementation into XmlTextReader as opposed to this hidden class? Am I
always going to be able to count on having ReadValueChunk() implemented if I
go the XmlReader.Create() route?
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
N

nickdu

I'm a bit confused now. You say that when you use the Create() method on the
abstract XmlReader class you'll get the XmlTextReader implementation if you
pass a stream argument? If that's the case then XmlReadValueChunk() won't
work if I'm using streams, correct?

It's a bit odd that I can't count on certain functionality being there.
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com


"Steven Cheng" said:
Hi Nick,

As Jeroen suggested, for createing xml reader/writer, we're recommended to
use the factory method(Create) on XmlReader/XmlWriter abstract class. There
are several benefits on this(such as reusable setting and xmlreader
chaining...), here is the MSDN document mentioned this:

#Creating XML Readers
http://msdn.microsoft.com/en-us/library/9khb6435.aspx


And by default XmlTextReader implementation is used for
XmlReader.Create(when you supply a stream based input). e.g.

===============================
static void RunXmlReader()
{
byte[] bytes = Encoding.UTF8.GetBytes(@"<testChuck>
Master pages in ASP.NET 2.0 applications
are the pages that enable you to provide a consistent look to your web
application. In ASP.NET 1.1, to achieve a consistent look across a website,
you need to use User controls and place them on each page. The master pages
eliminate the need to place the header, footer, or other important sections
on each page of your website repeatedly. The master pages are programmable
and contain methods, properties, and controls that can be made visible in
all other content pages. However, these elements need to be declared with
the Public scope in the master pages. You can build your content pages,
which have a unique content of their own and then merge the master page
with it to provide the page a consistent look. A single website can have
more than one master page. To use master page in your content page, you
need to reference the master page that you want to use. This article
discusses the way that you can use to reference a master page in your
content page.
</testChuck>"
);

MemoryStream ms = new MemoryStream(bytes);

XmlReaderSettings settings = new XmlReaderSettings();
XmlReader xtr = XmlReader.Create(ms);
Console.WriteLine(xtr.GetType());


xtr.ReadToFollowing("testChuck");
xtr.Read();

int cnt = 0;
char[] cbuf = new char[128];

cnt = xtr.ReadValueChunk(cbuf, 0, cbuf.Length);

while (cnt > 0)
{
Console.Write(new string(cbuf,0, cnt));
cnt = xtr.ReadValueChunk(cbuf, 0, cbuf.Length);
}

xtr.Close();
}
===============================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?bmlja2R1?= <[email protected]>
Subject: Support for stream based reading from XmlReader?
Date: Sun, 25 Jan 2009 11:49:02 -0800
I see XmlReader has a method named ReadValueChunk() which is a chunking
mechanism for reading large text from an XML document. However, that method
is not implemented in XmlTextReader. Is there some way to chunk the reads of
large textual data?
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
J

Jeroen Mostert

nickdu said:
I'm curious, why is it implemented this way? Why not just put the
implementation into XmlTextReader as opposed to this hidden class?

This would probably have required a substantial rewrite of the XmlTextReader
class, offering much opportunity for breaking things. 2.0 introduced the
..Create() factory method, which is a better idea than tying clients to one
specific concrete class. As part of the move to that, brand new classes were
written that offer better performance in addition to this new feature. 2.0
clients can and should use this, 1.x clients and the existing framework
parts can continue to instantiate XmlTextReader directly and get no benefit
(but no problems either).

In short, it's an opportunistic move on the part of Microsoft.
Am I always going to be able to count on having ReadValueChunk()
implemented if I go the XmlReader.Create() route?

No, this depends on the reader settings you pass. If you request validation,
the resulting reader will not support ReadValueChunk(). (The MSDN says "Text
parsing XmlReader objects that were created from the static Create method
always return true", but this is true only if "text parsing XmlReader
objects" somehow doesn't include validating ones.)

The documentation doesn't give specific guarantees as to which scenarios
will give you readers that are guaranteed to support .ReadValueChunk(),
which is why it makes sense to use .CanReadValueChunk to test for it so you
can fallback to a less efficient method or bail out, as is appropriate.
However, since Microsoft would be presumably be very reluctant to break
existing behavior, you should be safe assuming that readers created through
..Create() which do not request validation will support .ReadValueChunk(), as
this is how it's currently implemented.
 
S

Steven Cheng

Thanks for your reply Nick,

The "XmlTextReader implementation" I mentioned means the
"XmlTextReaderImpl" class, this is the TextReader implementaion (derived
from XmlReader), you can get it via the following code

================
XmlReaderSettings settings = new XmlReaderSettings();
XmlReader xtr = XmlReader.Create(ms);
Console.WriteLine(xtr.GetType());
================

Also, one thing important is that when you use the above code style(use the
"Create" factory method of XmlReader abstract class), you're following the
new .NET 2.0 XML reader/writer API pattern. While the XmlTextReader still
exists for backward compatibility. The .NET 2.0 XmlReader/Writer pattern
will not use it(XmlTextReader/Writer) internally.

Therefore, you can rely on the abstract Create factory method of the base
class and get the proper implementation and call those supported methods on
the returned reader/writer instance.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

--------------------
From: =?Utf-8?B?bmlja2R1?= <[email protected]>
References: <[email protected]>
 
N

nickdu

Thanks. I can understand providing another class so that you don't introduce
any breaking changes. However, if in the future they make changes to
XmlTextReaderImpl then they have the same problem of introducing breaking
changes. Though from the looks of this factory interface it appears they can
give you different versions based on settings you pass in. So if in the
future they add new functionality they could do so in a new implementation
class and return that so as to not introduce any breaking changes. Though
maintenance of all these versions could be an issue (for them (MSFT) of
course).
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top