Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : strange and curious problem


bch NO[at]SPAM genie.co.uk
2/18/2005 3:16:07 AM
Hello and thank you for reading on (hopefully).

How does one typecast the XMLREADER returned from the XSLTRANSFORM
method 'transform' into XMLTEXTREADER, so it can be passed in to an
XMLVALIDATINGREADER?

I am getting an invalid cast.

It seems that although the XSLTRANSFORM 'transform' method does return
a XMLREADER object and you can work with that, you can not pass it
into the XMLVALIDATINGREADER construct to allow validation of the
resultant xml.


Thanks for any commments.

bch NO[at]SPAM genie.co.uk
2/18/2005 12:11:55 PM
[quoted text, click to view]


Thank you for the reply.
Excuse the dirty VB code/syntax/spelling (just to keep it brief and
for example only)

dim xpd as xpathdocument
dim xr as xmlreader
dim xvr as xmlvalidatingreader
dim xsl as xsltransform

xpd = new xpathdocument("http://bla bla")

xsl = new xsltransform()
xsl.load("http://bla bla bla")

xr = xsl.transform(xpd, nothing)

xvr = new xmlvalidatingreader(xr) << this fails !


it seems that xmlvalidatingreader constructor expects xmlreader - but
only as a xmltextreader (in the current dotnet implementation).

so tried to typecast (i used all different ways - this is one) just as
a check, even though it should not be required i agree :

xvr = new xmlvalidatingreader(ctype(xr,xmltextreader)) << this
fails too


anyway, as i was wanting to validate the resultant transfored xml it
looks like i can not do it at present. or have i missed a trick.

Martin Honnen
2/18/2005 1:55:52 PM


[quoted text, click to view]


[quoted text, click to view]

When I look at the API documentation then both allow an XmlReader so
simply use that, I am not sure where you need or want to cast:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxslxsltransformclasstransformtopic6.asp>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlValidatingReaderClassctorTopic1.asp>


If you still have problems, show us the relevant code snippets so what
we can see what exactly you are trying to cast to what.



--

Martin Honnen
Nick Malik [Microsoft]
2/19/2005 4:19:23 PM
That's a strange problem, alright. I never went looking for this one
before, but I'm sure it can be done.

I spent a few minutes digging around the framework and no easy solution
popped out.

One idea: Create an XML Document using the reader that results from the XSL
Transform. Then write the document to a memory stream. Then read the
memory stream using an XMLValidatingReader.

Seems kinda round-about. If anyone has a better idea: please step up!

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
[quoted text, click to view]

Martin Honnen
2/20/2005 6:14:16 PM


[quoted text, click to view]


[quoted text, click to view]

I see the problem now, looks a bit like a design flaw then, the
Transform method returns an XmlReader by signature and the
XmlValidatingReader takes one by its signature but then in its
documentation says it throws an illegal argument execption if the
argument is not an XmlTextReader and that is what happens. As the
Transform method (at least as tested here with .NET 1.1) returns an
object of type System.Xml.Xsl.ReaderOutput which doesn't allow a cast to
XmlTextReader there is no way then to simply chain the XmlReader result
of a Transform call to an XmlValidatingReader.

It seems you are better off transforming to a stream and consume that
with an XmlTextReader consumed by an XmlValidatingReader e.g. (C#)

MemoryStream xsltOutput = new MemoryStream();
xsltProcessor.Transform(xsltInput, null, xsltOutput);
xsltOutput.Position = 0;

XmlValidatingReader xmlValidator = new XmlValidatingReader(new
XmlTextReader(xsltOutput));

--

Martin Honnen
AddThis Social Bookmark Button