473,666 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(x mlStream, Encoding.UTF8)
' Dim xmlWriter As New XmlTextWriter(C onsole.Out)

xmlWriter.Forma tting = Formatting.Inde nted

xmlWriter.Write StartDocument()
xmlWriter.Write StartElement("E NTRY_DTL")
xmlWriter.Write ElementString(" ORG_CODE", "10")
xmlWriter.Write ElementString(" FISCAL_YR",
XmlConvert.ToSt ring(2003))
xmlWriter.Write EndElement()
xmlWriter.Write EndDocument()
Try
doc.Load(xmlStr eam)
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 9072
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(x mlStream, Encoding.UTF8)
' Dim xmlWriter As New XmlTextWriter(C onsole.Out)

xmlWriter.Forma tting = Formatting.Inde nted

xmlWriter.Write StartDocument()
xmlWriter.Write StartElement("E NTRY_DTL")
xmlWriter.Write ElementString(" ORG_CODE", "10")
xmlWriter.Write ElementString(" FISCAL_YR",
XmlConvert.ToSt ring(2003))
xmlWriter.Write EndElement()
xmlWriter.Write EndDocument()
Try
Insert here
xmlStream.Posit ion=0
doc.Load(xmlStr eam)
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.Posit ion = 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_P LEASEtkachenko. com> wrote in message news:<e2******* *******@tk2msft ngp13.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(x mlStream, Encoding.UTF8)
' Dim xmlWriter As New XmlTextWriter(C onsole.Out)

xmlWriter.Forma tting = Formatting.Inde nted

xmlWriter.Write StartDocument()
xmlWriter.Write StartElement("E NTRY_DTL")
xmlWriter.Write ElementString(" ORG_CODE", "10")
xmlWriter.Write ElementString(" FISCAL_YR",
XmlConvert.ToSt ring(2003))
xmlWriter.Write EndElement()
xmlWriter.Write EndDocument()
Try


Insert here
xmlStream.Posit ion=0
doc.Load(xmlStr eam)
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
12026
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 will rehydrate my class. I've built a little Windows forms test app to see if my approach will work. It doesn't. When I step through the code in the debugger, I see that the MemoryStream is null, and the string variable "xmlFragment" is an empty
3
8305
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 user this object for this purpose? Thanks, Dave MemoryStream stm = new MemoryStream() XmlTextWriter writer = new XmlTextWriter(stm, System.Text.Encoding.UTF8) writer.WriteStartDocument() writer.WriteComment("This Is A List of My Books")...
2
2059
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 System.IO.MemoryStream(); System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(ms, System.Text.Encoding.Unicode);
12
2129
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 = New XslTransform()
4
11514
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 having issues using XmlTextWriter, saving it out to a file with UTF8 encoding, and seeing "dirty", or "human unreadable" characters show up *right before* the XML declaration. I need to have the XML declaration state "encoding = utf-8", but also...
1
10190
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 the document with PreserveWhiteSpace = true.) I thought I could do this by using an XmlTextWriter with Formatting set to Formatting.Indented, but this doesn't work. The Formatting.Indented on the XmlTextWriter only indents my output when I...
8
10439
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 on closing my stream, and I can't convince it not to. A much-simplified example is below; basically, as soon as the writer is disposed (marked **** below) the base stream gets closed - which is a pain if I was still using it ;-p The base...
4
3115
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 XmlTextWriter(xmlFileName, null); xmlWriter.Namespaces = false;
3
6264
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... XmlTextWriter mytw = new XmlTextWriter("c:\\temp\\myfile.xml", null) Everything writes properly and a good XML document gets created. However if I instantiate the XmlTextWriter to a memoryStream...
0
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.