473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

xmlTextWriter to Stream to xmldoc produces null stream?

I have a dataset from which I need to extract certain columns, format
them into xml and output to an xmldocument object.

From reading various posts on similar subjects, I have come up with
the following test code (using 2 constants, rather than my dataset
values):

Public Sub test()
Dim doc As New XmlDocument
Dim xmlStream As Stream = New MemoryStream
Dim xmlWriter As New XmlTextWriter(xmlStream, Encoding.UTF8)
' Dim xmlWriter As New XmlTextWriter(Console.Out)

xmlWriter.Formatting = Formatting.Indented

xmlWriter.WriteStartDocument()
xmlWriter.WriteStartElement("ENTRY_DTL")
xmlWriter.WriteElementString("ORG_CODE", "10")
xmlWriter.WriteElementString("FISCAL_YR",
XmlConvert.ToString(2003))
xmlWriter.WriteEndElement()
xmlWriter.WriteEndDocument()
Try
doc.Load(xmlStream)
Catch ex As Exception
MsgBox(ex, ex.Message)
End Try
End Sub

However the doc.load throws a 'missing root' exception and, on closer
inspection, xmlStream is a null stream. The commented line shows me
the writer is constructing the xml to Console OK, but it doesnt seem
to go into the MemoryStream at all.

Can anyone offer any assistance to an XML virgin who's struggling.

As an aside, am I just going about this the wrong way and would be
better off applying a dataview to my dataset, to get the required
columns, and then somehow writing that directly to an xmldoc?

Thanks,
Nov 11 '05 #1
2 9057
Steve Gilbey wrote:
From reading various posts on similar subjects, I have come up with
the following test code (using 2 constants, rather than my dataset
values):

Public Sub test()
Dim doc As New XmlDocument
Dim xmlStream As Stream = New MemoryStream
Dim xmlStream As MemoryStream = New MemoryStream
Dim xmlWriter As New XmlTextWriter(xmlStream, Encoding.UTF8)
' Dim xmlWriter As New XmlTextWriter(Console.Out)

xmlWriter.Formatting = Formatting.Indented

xmlWriter.WriteStartDocument()
xmlWriter.WriteStartElement("ENTRY_DTL")
xmlWriter.WriteElementString("ORG_CODE", "10")
xmlWriter.WriteElementString("FISCAL_YR",
XmlConvert.ToString(2003))
xmlWriter.WriteEndElement()
xmlWriter.WriteEndDocument()
Try
Insert here
xmlStream.Position=0
doc.Load(xmlStream)
Catch ex As Exception
MsgBox(ex, ex.Message)
End Try


btw, you can write directly to XmlDocument using Chris Lovett's XmlNodeWriter
class (find it in gotdotnet.com).
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
Thanks Oleg,
Your suggestion, plus one from elsewhere got me going.

It seems it wasn't enough to just position the stream, the stream and
the writer also need to be flushed before I got anything into Doc.
Adding this:
xmlWriter.Flush()
xmlStream.Flush()
xmlStream.Position = 0

before the doc.load fixed it. Also note, it needs Position AFTER the
flush. The other way around doesn't work.

However in the meantime, I followed your suggestion and got the
XmlNodeWriter and am now using that instead!

Rgds
Steve

Oleg Tkachenko <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message news:<e2**************@tk2msftngp13.phx.gbl>...
Steve Gilbey wrote:
From reading various posts on similar subjects, I have come up with
the following test code (using 2 constants, rather than my dataset
values):

Public Sub test()
Dim doc As New XmlDocument
Dim xmlStream As Stream = New MemoryStream


Dim xmlStream As MemoryStream = New MemoryStream
Dim xmlWriter As New XmlTextWriter(xmlStream, Encoding.UTF8)
' Dim xmlWriter As New XmlTextWriter(Console.Out)

xmlWriter.Formatting = Formatting.Indented

xmlWriter.WriteStartDocument()
xmlWriter.WriteStartElement("ENTRY_DTL")
xmlWriter.WriteElementString("ORG_CODE", "10")
xmlWriter.WriteElementString("FISCAL_YR",
XmlConvert.ToString(2003))
xmlWriter.WriteEndElement()
xmlWriter.WriteEndDocument()
Try


Insert here
xmlStream.Position=0
doc.Load(xmlStream)
Catch ex As Exception
MsgBox(ex, ex.Message)
End Try


btw, you can write directly to XmlDocument using Chris Lovett's XmlNodeWriter
class (find it in gotdotnet.com).

Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
Hi, I'm stumped and could use some suggestions. I'm trying to serialize some data to an XML fragment in string form, then deserialize it from a string back to an XML fragment, from which I...
3
by: Dave | last post by:
Hi, I'm trying to create an XML document with XMLTextWriter but I want to store it in memory (not write it to a file like so many examples do). I tried the following but with no luck. Should I not...
2
by: vector | last post by:
Here is a function more or less exactly as I found it from somewhere on the internet. static string BeautifyXML(string sXML) { string result = ""; System.IO.MemoryStream ms = new...
12
by: Amanda | last post by:
I have tried everything with this! I get an error stating "Index was outside the bounds of the array" My code looks like this.... xmlDoc = New XmlDocument() xmlDoc.Load("xml.doc") xslDoc...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
4
by: quest | last post by:
Is there anyway I can generate the xml in the following format using XmlTextWriter ? Intended output: <?xml version="1.0" ?> I tried: XmlTextWriter xmlWriter = new...
3
by: GaryDean | last post by:
I'm using an XmlTextWriter and it's various methods such as WriteElementString, WriteStartElement, WriteEndElement, etc to create an xml document. When I instantiate the XmlTextWriter to a file......
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.