Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2003

Tip: Looking for answers? Try searching our database.

xmlTextWriter to Stream to xmldoc produces null stream?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve Gilbey - 08 Oct 2003 12:32 GMT
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,
Oleg Tkachenko - 08 Oct 2003 13:53 GMT
> From reading various posts on similar subjects, I have come up with
> the following test code (using 2 constants, rather than my dataset
[quoted text clipped - 3 lines]
>     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)
[quoted text clipped - 9 lines]
>     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).
Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Steve Gilbey - 09 Oct 2003 09:40 GMT
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

> > From reading various posts on similar subjects, I have come up with
> > the following test code (using 2 constants, rather than my dataset
[quoted text clipped - 30 lines]
> btw, you can write directly to XmlDocument using Chris Lovett's XmlNodeWriter
> class (find it in gotdotnet.com).

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.