I have a question that I hope that you guys can answer. :) I am writing a Class that will be used to produce a XML file (report elements) that will be stored in a SQLServer table which will hold the XML until a printing process (I don't have to worry about
this) comes along and prints the forms based on the string of XML that my process has stored in the table. Currently I am going down the route of using the TextWriter to write to a file, but with the rollout of this application there is a possiblility of this
being run at the same time, so I am looking at just storing the string in virtual memory until it is stored in the DB. How would I accomplish this, if it can be done? Here is the code I have so far, but I am looking to check to see if my theory is on the right
track more than my code at this point. However code help never hurts...
Dim tw As XmlTextWriter
Dim fs As FileStream
Try
tw = New XmlTextWriter(fs, New System.Text.UTF8Encoding())
tw.Formatting = Formatting.Indented
tw.Indentation = 4
tw.WriteStartDocument()
....etc
This was working fine when I had an actual file name in the place of fs.
Dim testing As New XmlDocument()
testing.Load(fs)
strLine = testing.OuterXml
This was my theory of getting the XML file to a string... TIA
You are now using FileStream as target of the XML writing. However you could use MemoryStream instead FileStream. ie. write XML to MemoryStream using XmlTextWriter. Then you could get the XML as string by reading MemoryStream using StreamReader and especially
StreamReader's ReadToEnd method that returns contents of the stream to be readed as string.
Thanks, that seems to work at least while stepping through the code, however I am ending up with an empty string at the end of the process... this is my entire sub, I know I am missing something small.
Public Sub writeHeader()
Dim strXML As String
Dim tw As XmlTextWriter
Dim ms As MemoryStream
Dim sReader As StreamReader
Try
ms = New MemoryStream()
tw = New XmlTextWriter(ms, New System.Text.UTF8Encoding())
tw.Formatting = Formatting.Indented
tw.Indentation = 4
tw.WriteStartDocument()
tw.WriteComment("Produced from Class on " & Now.ToString)
tw.WriteStartElement("Workpaper")
tw.WriteStartElement("ACCOUNT")
tw.WriteAttributeString("FUNDNAME", Trim(fundName))
tw.WriteAttributeString("ORGLONGNAME", Trim(_dsSQL.Tables(0).Rows(0)("ORG_LONG_NAME")))
tw.WriteAttributeString("ACCOUNTNBR", Trim(_dsSQL.Tables(0).Rows(0)("ACCOUNT_NBR10")))
tw.WriteAttributeString("ORGMGRNAME", Trim(_dsSQL.Tables(0).Rows(0)("ORG_MANAGER_NAME")))
tw.WriteEndElement()
tw.WriteEndDocument()
sReader = New StreamReader(ms)
strXML = sReader.ReadToEnd
tw.Flush()
tw.Close()
Catch e As Exception
RaiseError(e)
End Try
End Sub
Two code changes should help: 1) Move the Flush to before you create the StreamReader (the TextWriter won't write to the MemoryStream until you either Flush or Close -- which forces a Flush) 2) (aka, the magical one): After flushing, move back to the beginning
of the MemoryStream: ms.Position = 0 So, the end result (not including all code, just the relevant parts): tw.WriteEndDocument() tw.Flush() ms.Position = 0 sReader = new StreamReader(ms) strXML = sReader.ReadToEnd() tw.Close()
Was just going to say that after writing to MemoryStream it's position needs to be reset. Just don't close the XMLTextWriter (call it's Close method) before reading MemoryStream with StreamReader, because calling the close will close the underlying stream (MemoryStream
in this case) also and you don't want that (you had it right but just reminded) Ksharkey's code shows exactly what needs to be done.
EOJRR1
Member
15 Points
3 Posts
XML file question
Mar 26, 2003 04:53 PM|LINK
Dim tw As XmlTextWriter Dim fs As FileStream Try tw = New XmlTextWriter(fs, New System.Text.UTF8Encoding()) tw.Formatting = Formatting.Indented tw.Indentation = 4 tw.WriteStartDocument() ....etcThis was working fine when I had an actual file name in the place of fs.Dim testing As New XmlDocument() testing.Load(fs) strLine = testing.OuterXmlThis was my theory of getting the XML file to a string... TIAjoteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: XML file question
Mar 26, 2003 06:22 PM|LINK
Teemu Keiski
Finland, EU
EOJRR1
Member
15 Points
3 Posts
Re: XML file question
Mar 26, 2003 07:30 PM|LINK
Public Sub writeHeader() Dim strXML As String Dim tw As XmlTextWriter Dim ms As MemoryStream Dim sReader As StreamReader Try ms = New MemoryStream() tw = New XmlTextWriter(ms, New System.Text.UTF8Encoding()) tw.Formatting = Formatting.Indented tw.Indentation = 4 tw.WriteStartDocument() tw.WriteComment("Produced from Class on " & Now.ToString) tw.WriteStartElement("Workpaper") tw.WriteStartElement("ACCOUNT") tw.WriteAttributeString("FUNDNAME", Trim(fundName)) tw.WriteAttributeString("ORGLONGNAME", Trim(_dsSQL.Tables(0).Rows(0)("ORG_LONG_NAME"))) tw.WriteAttributeString("ACCOUNTNBR", Trim(_dsSQL.Tables(0).Rows(0)("ACCOUNT_NBR10"))) tw.WriteAttributeString("ORGMGRNAME", Trim(_dsSQL.Tables(0).Rows(0)("ORG_MANAGER_NAME"))) tw.WriteEndElement() tw.WriteEndDocument() sReader = New StreamReader(ms) strXML = sReader.ReadToEnd tw.Flush() tw.Close() Catch e As Exception RaiseError(e) End Try End SubThanks.ksharkey
Member
249 Points
51 Posts
Re: XML file question
Mar 26, 2003 09:17 PM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: XML file question
Mar 27, 2003 05:17 AM|LINK
Teemu Keiski
Finland, EU
EOJRR1
Member
15 Points
3 Posts
Re: XML file question
Mar 27, 2003 11:48 AM|LINK
Proximo
Member
240 Points
49 Posts
Re: XML file question
Aug 12, 2003 08:18 PM|LINK