Project

Profile

Help

saxon9API dispose of file

Added by Anonymous over 16 years ago

Legacy ID: #4757588 Legacy Poster: pvallone (pvallone)

Hi, I am using the saxon9API to perform some XQuery. I am using the C# example that came with the API. Everything is working fine except that the following code doesn't let me open the output file until I quit the application. This indicates to me I am not disposing of the API correctly. Here is my VB code. Public Shared Sub ExampleFromXmlReader(ByVal inputFileName As String, ByVal outputfile As String) Try Dim processor As Processor = New Processor() Dim reader As XmlTextReader = New XmlTextReader(inputFileName, New FileStream(inputFileName, FileMode.Open, FileAccess.Read)) reader.Normalization = True ' add a validating reader - not to perform validation, but to expand entity references Dim validator As XmlReader = XmlReader.Create(reader, Nothing) Dim doc As XdmNode = processor.NewDocumentBuilder().Build(validator) Dim compiler As XQueryCompiler = processor.NewXQueryCompiler() compiler.BaseUri = "www.mynamespace.com" compiler.DeclareNamespace("ACM", "www.mynamespace.com") Dim exp As XQueryExecutable = compiler.Compile("//ACM:Oproc") Dim eval As XQueryEvaluator = exp.Load() eval.ContextItem = doc Dim qout As Serializer = New Serializer() qout.SetOutputProperty(Serializer.METHOD, "xml") qout.SetOutputProperty(Serializer.INDENT, "yes") qout.SetOutputStream(New FileStream(outputfile, FileMode.Create, FileAccess.Write)) eval.Run(qout) ' Clean up qout.Close() reader.Close() validator.Close() Catch ex As Exception MsgBox(Err.Description, MsgBoxStyle.Critical) End Try End Sub Thanks for the help. Regards pvallone


Replies (6)

Please register to reply

RE: saxon9API dispose of file - Added by Anonymous over 16 years ago

Legacy ID: #4758081 Legacy Poster: Michael Kay (mhkay)

The general principle followed by this API is that if Saxon creates the stream, Saxon will close it; but if the user creates the stream, then the user must close it. Your code fragment here creates two FileStream objects, so you should close them both after use. (In the case of the output stream, this allows you to have several transformations append successively to the same stream if you wish.) Michael Kay

RE: saxon9API dispose of file - Reuse Input ? - Added by Anonymous over 16 years ago

Legacy ID: #4758463 Legacy Poster: David Lee (daldei)

Does Saxon read the EOF on the Input stream ? That is, if I had an input stream with 2 documents ( say 2 XML documents concatenated into a file or string ), could I read them both, by calling buildDocument() twice in a row reusing the same input stream ? Theoretically, saxon could stop when it read the final ">" instead of consuming more input data thus allowing streamts to contain multiple documents. -David

RE: saxon9API dispose of file - Reuse Input ? - Added by Anonymous over 16 years ago

Legacy ID: #4758481 Legacy Poster: Michael Kay (mhkay)

Saxon doesn't read the input stream - it passes it to the XML parser. I'm not aware of any XML parsers that allow multiple XML documents in a single file. I don't think the XML specification makes it illegal, but it does nothing to encourage it.

RE: saxon9API dispose of file - Added by Anonymous over 16 years ago

Legacy ID: #4758544 Legacy Poster: pvallone (pvallone)

Hi, Thank you for the help. I am not sure what I am missing then. I believe I have closed both the streams. qout.Close() reader.Close() What am I missing? Thanks for the help. pvallone

RE: saxon9API dispose of file - Added by Anonymous over 16 years ago

Legacy ID: #4758570 Legacy Poster: Michael Kay (mhkay)

You closed the Serializer, but not the Stream that it uses: Dim qout As Serializer = New Serializer() qout.SetOutputStream(New FileStream(outputfile, FileMode.Create, FileAccess.Write)) ' Clean up qout.Close() Closing the Serializer doesn't close the FileStream. It would if you left Saxon to create the FileStream, by using qout.SetOutputFile() instead. Michael Kay

RE: saxon9API dispose of file - Added by Anonymous over 16 years ago

Legacy ID: #4758575 Legacy Poster: pvallone (pvallone)

Thank you Michael.

    (1-6/6)

    Please register to reply