Using XMLDocument.Load with a memory stream...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am looking for examples that would illustrate something similar to the
following requirements:

I am building an in-memory xml structure using the following as the setup
and classes to perform this step:

If fileOutput Then
Dim XmlWriter As New XmlTextWriter("D:\QICS_MT\Debug Output\" & _
fileName, System.Text.Encoding.UTF8)
XmlWriterXsdMultiLevel(regexObject, _
elementCollection, _
XmlWriter)
Else
Dim coreStream As New MemoryStream
Dim XmlStreamWriter As New StreamWriter(coreStream,Text.Encoding.UTF8)
Dim XmlWriter As New XmlTextWriter(XmlStreamWriter)
XmlWriterXsdMultiLevel(regexObject, elementCollection, XmlWriter)
End If

If I take the upper branch I can successfully create and write the document
and subsequently I can open the resulting Xml file and validate it against an
external xsd file.

I would like to be able to take the lower branch and create the same xml
structure, yet in this instance I would like to be able to pass the created
xml content to an XmDocument class, so that I can pass and use it elsewhere
in the application.

I am having problems understanding how to use the XmlDocument.Load(Stream)
method, though I believe this is how I should do this. I have not found many
examples of using this method, so I was looking for assistance in that area.

Thanks,
Tim
 
TDNeumann said:
I am looking for examples that would illustrate something similar to the
following requirements:

I am building an in-memory xml structure using the following as the setup
and classes to perform this step:

If fileOutput Then
Dim XmlWriter As New XmlTextWriter("D:\QICS_MT\Debug Output\" & _
fileName, System.Text.Encoding.UTF8)
XmlWriterXsdMultiLevel(regexObject, _
elementCollection, _
XmlWriter)
Else
Dim coreStream As New MemoryStream
Dim XmlStreamWriter As New StreamWriter(coreStream,Text.Encoding.UTF8)
Dim XmlWriter As New XmlTextWriter(XmlStreamWriter)
XmlWriterXsdMultiLevel(regexObject, elementCollection, XmlWriter)
End If

If I take the upper branch I can successfully create and write the document
and subsequently I can open the resulting Xml file and validate it against an
external xsd file.

I would like to be able to take the lower branch and create the same xml
structure, yet in this instance I would like to be able to pass the created
xml content to an XmDocument class, so that I can pass and use it elsewhere
in the application.

I am having problems understanding how to use the XmlDocument.Load(Stream)
method, though I believe this is how I should do this. I have not found many
examples of using this method, so I was looking for assistance in that area.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that if you're passing in a memory stream which has been written
to, and you want to read the data which has been written, you'll need
to position the stream back at the start, eg with

stream.Position = 0
 
Here is a rough outline of what I would like to do...

Public Function ProcessMessage(...) As XmlDocument
Dim coreStream As New MemoryStream
Dim XmlStreamWriter As New StreamWriter(coreStream,Text.Encoding.UTF8)
Dim XmlWriter As New XmlTextWriter(XmlStreamWriter)
Dim MyDocument as New XmlDocument
MyDocument = XmlBuilder(regexObject, elementCollection, XmlWriter)
End Function

Private Function XmlWriterXsdMultiLevel( _
ByVal regexObject As Regex, _
ByVal elementCollection As MatchCollection, _
ByVal XmlWriter As XmlTextWriter) As XmlDocument

...
... Build XmlContent from MatchCollection
...

XmlWriter.BaseStream.Position = 0
Dim TemporaryXmlDocument As New XmlDocument
TemporaryXmlDocument.Load(stream...)
XmlBuilder = TemporaryXmlDocument
.Close()
End Function

I think this represents what it is I want to do, though I am sure the
implementation is incorrect. Basically I need to be able to build an
in-memory Xml file and then copy that into an XmlDocument, with the end goal
of being able to use that with an xsd validator and then be able to
manipulate the contents of the document.

Thanks,
Tim
 
TDNeumann said:
Here is a rough outline of what I would like to do...

Public Function ProcessMessage(...) As XmlDocument
Dim coreStream As New MemoryStream
Dim XmlStreamWriter As New StreamWriter(coreStream,Text.Encoding.UTF8)
Dim XmlWriter As New XmlTextWriter(XmlStreamWriter)
Dim MyDocument as New XmlDocument
MyDocument = XmlBuilder(regexObject, elementCollection, XmlWriter)
End Function

Private Function XmlWriterXsdMultiLevel( _
ByVal regexObject As Regex, _
ByVal elementCollection As MatchCollection, _
ByVal XmlWriter As XmlTextWriter) As XmlDocument

...
... Build XmlContent from MatchCollection
...

XmlWriter.BaseStream.Position = 0
Dim TemporaryXmlDocument As New XmlDocument
TemporaryXmlDocument.Load(stream...)
XmlBuilder = TemporaryXmlDocument
.Close()
End Function

I think this represents what it is I want to do, though I am sure the
implementation is incorrect. Basically I need to be able to build an
in-memory Xml file and then copy that into an XmlDocument, with the end goal
of being able to use that with an xsd validator and then be able to
manipulate the contents of the document.

It doesn't look particularly wrong to me. If you've tried it and are
having problems, then please provide a complete program as I suggested
before.

(Do you really need to pass in the XmlTextWriter as a parameter? Why
not just create the stream within the method?)
 
In this example it would not be nec., where it originally started was in
wanting the ability to allow the calling application to send in a fileOutput
flag thereby allowing me to output the xml files for debug purposes. I could
rework that to take it out...what I need to accomplish here, from a 10,000
foot perspective, is the following:

I am receiving a text message with a pre-defined format, that message is
parsed using a regular expression and then pass to this class which take the
regular expression elements and their groups and builds it into a well formed
xml file. When I output these files I am able to load them into oXygen and
perform both an internal and external xsd validation of the file
successfully. I have an additional class which loads the file as and
XmlDocument and reads through the file, building a collection of stored
procedure parameters and call the sp through the Microsoft Data Access Block.
The missing connection between these components is the abiltity to take this
xml structure and turn it into an XmlDocument in memory. At which point I
would pass that XmlDocument into the stored procedure builder directly.

Thanks,
Tim
 
TDNeumann said:
In this example it would not be nec., where it originally started was in
wanting the ability to allow the calling application to send in a fileOutput
flag thereby allowing me to output the xml files for debug purposes. I could
rework that to take it out...what I need to accomplish here, from a 10,000
foot perspective, is the following:

I am receiving a text message with a pre-defined format, that message is
parsed using a regular expression and then pass to this class which take the
regular expression elements and their groups and builds it into a well formed
xml file. When I output these files I am able to load them into oXygen and
perform both an internal and external xsd validation of the file
successfully. I have an additional class which loads the file as and
XmlDocument and reads through the file, building a collection of stored
procedure parameters and call the sp through the Microsoft Data Access Block.
The missing connection between these components is the abiltity to take this
xml structure and turn it into an XmlDocument in memory. At which point I
would pass that XmlDocument into the stored procedure builder directly.

Okay, well, it looks like you're all set to go. If you have any
problems with it, please post appropriate code and we'll see what we
can do.
 
Back
Top