473,508 Members | 2,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Write XML

Hi,

I've created a simple application that reads data from a
SQL Server, and appends this to an XML file.

The problem is, that the XML data structure seems to go to
the top level when I write the file. By this, I mean, if
my original XML file looks like...

<?xml version="1.0" encoding="utf-8" ?>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mCreator>Demonstration Site</mCreator>
<mPriority>1</mPriority>
<mSubject>Test Subject</mSubject>
<mDateReceived>07/08/2003
13:13:00</mDateReceived>
</message>
</inbox>

Then when I run the following code...

Dim dReader As SqlClient.SqlDataReader
Dim dsMessage As New DataSet("inbox")
Dim daMessage As New
System.Data.SqlClient.SqlDataAdapter()

SqlConnection1.Open()
sqlRetrieveMessageDetails.Parameters
("@mUID").Value = Request.QueryString("mUID")
daMessage.SelectCommand = sqlRetrieveMessageDetails
daMessage.Fill(dsMessage, "message")
dsMessage.AcceptChanges()

dReader = sqlRetrieveMessageDetails.ExecuteReader
While dReader.Read
txtFrom.Text = dReader(3)
txtTo.Text = "This to be filled in"
txtSubject.Text = dReader(5)
txtDateSent.Text = dReader(4)
celMessageDetails.InnerHtml = dReader(6)
End While
dReader.Close()
SqlConnection1.Close()

If Request.QueryString("new") = "Yes" Then
Dim fileName As String
= "c:\inetpub\wwwroot\hbitl_umc\testInbox.xml"
Dim newStream As New System.IO.FileStream
(fileName, IO.FileMode.Append)
Dim xmlWriter As New System.Xml.XmlTextWriter
(newStream, System.Text.Encoding.ASCII)
dsMessage.WriteXml(xmlWriter)
xmlWriter.Close()
End If

My XML file turns to...

<?xml version="1.0" encoding="utf-8" ?>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mCreator>Empower Systems Demonstration
Site</mCreator>
<mPriority>1</mPriority>
<mSubject>Test Subject</mSubject>
<mDateReceived>07/08/2003
13:13:00</mDateReceived>
</message>
</inbox>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mPriority>1</mPriority>
<mCreator>Demonstration Site</mCreator>
<mDateCreated>2003-08-
07T13:13:00.0000000+01:00</mDateCreated>
<mSubject>Test Subject</mSubject>
<mDetails>&lt;B&gt;This is a
&lt;I&gt;test&lt;/I&gt; message&lt;/B&gt;</mDetails>
</message>
</inbox>

As you can see, the <inbox> root element is duplicated!
The new message element, and it's children, need to be
placed inside the <inbox> parent.

Can someone shed some light as to what I'm doing wrong.

TIA
Nov 11 '05 #1
2 3094
I think that you're having a problem because there is nothing special about
an XmlTextWriter.

The approach that I would take is; create a second dataset by loading the
Xml from the file (DataSet.ReadXml), merge that dataset with the dataset
that you created with the database query (Dataset.Merge), and finally write
the resulting dataset to disk.

"jason hirst" <ja*********@hotmail.com> wrote in message
news:0e****************************@phx.gbl...
Hi,

I've created a simple application that reads data from a
SQL Server, and appends this to an XML file.

The problem is, that the XML data structure seems to go to
the top level when I write the file. By this, I mean, if
my original XML file looks like...

<?xml version="1.0" encoding="utf-8" ?>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mCreator>Demonstration Site</mCreator>
<mPriority>1</mPriority>
<mSubject>Test Subject</mSubject>
<mDateReceived>07/08/2003
13:13:00</mDateReceived>
</message>
</inbox>

Then when I run the following code...

Dim dReader As SqlClient.SqlDataReader
Dim dsMessage As New DataSet("inbox")
Dim daMessage As New
System.Data.SqlClient.SqlDataAdapter()

SqlConnection1.Open()
sqlRetrieveMessageDetails.Parameters
("@mUID").Value = Request.QueryString("mUID")
daMessage.SelectCommand = sqlRetrieveMessageDetails
daMessage.Fill(dsMessage, "message")
dsMessage.AcceptChanges()

dReader = sqlRetrieveMessageDetails.ExecuteReader
While dReader.Read
txtFrom.Text = dReader(3)
txtTo.Text = "This to be filled in"
txtSubject.Text = dReader(5)
txtDateSent.Text = dReader(4)
celMessageDetails.InnerHtml = dReader(6)
End While
dReader.Close()
SqlConnection1.Close()

If Request.QueryString("new") = "Yes" Then
Dim fileName As String
= "c:\inetpub\wwwroot\hbitl_umc\testInbox.xml"
Dim newStream As New System.IO.FileStream
(fileName, IO.FileMode.Append)
Dim xmlWriter As New System.Xml.XmlTextWriter
(newStream, System.Text.Encoding.ASCII)
dsMessage.WriteXml(xmlWriter)
xmlWriter.Close()
End If

My XML file turns to...

<?xml version="1.0" encoding="utf-8" ?>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mCreator>Empower Systems Demonstration
Site</mCreator>
<mPriority>1</mPriority>
<mSubject>Test Subject</mSubject>
<mDateReceived>07/08/2003
13:13:00</mDateReceived>
</message>
</inbox>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mPriority>1</mPriority>
<mCreator>Demonstration Site</mCreator>
<mDateCreated>2003-08-
07T13:13:00.0000000+01:00</mDateCreated>
<mSubject>Test Subject</mSubject>
<mDetails>&lt;B&gt;This is a
&lt;I&gt;test&lt;/I&gt; message&lt;/B&gt;</mDetails>
</message>
</inbox>

As you can see, the <inbox> root element is duplicated!
The new message element, and it's children, need to be
placed inside the <inbox> parent.

Can someone shed some light as to what I'm doing wrong.

TIA

Nov 11 '05 #2
You are writing XML as if it were just text.

If you want to prune and graft the XML document, then you need to do
something other than
- open it as a text file for append
- append the additional XML

For example, you could
- load the existing XML file into a Dom document eg:
Dim doc As System.Xml.XmlDocument
doc.Load(filename)

- navigate via the DOM, eg
Dim root As System.Xml.XmlNode = doc.DocumentElement
Dim dsDoc As System.Xml.XmlDocument
dsDoc.LoadXml(dsMessage.GetXml())

root.InsertAfter(dsDoc.DocumentElement, root.FirstChild)

- write the changed document back to the disk
(as you have below but without the Append attribute)
see for example:
http://msdn.microsoft.com/library/en...AfterTopic.asp
-Dino

"jason hirst" <ja*********@hotmail.com> wrote in message
news:0e****************************@phx.gbl...
Hi,

I've created a simple application that reads data from a
SQL Server, and appends this to an XML file.

The problem is, that the XML data structure seems to go to
the top level when I write the file. By this, I mean, if
my original XML file looks like...

<?xml version="1.0" encoding="utf-8" ?>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mCreator>Demonstration Site</mCreator>
<mPriority>1</mPriority>
<mSubject>Test Subject</mSubject>
<mDateReceived>07/08/2003
13:13:00</mDateReceived>
</message>
</inbox>

Then when I run the following code...

Dim dReader As SqlClient.SqlDataReader
Dim dsMessage As New DataSet("inbox")
Dim daMessage As New
System.Data.SqlClient.SqlDataAdapter()

SqlConnection1.Open()
sqlRetrieveMessageDetails.Parameters
("@mUID").Value = Request.QueryString("mUID")
daMessage.SelectCommand = sqlRetrieveMessageDetails
daMessage.Fill(dsMessage, "message")
dsMessage.AcceptChanges()

dReader = sqlRetrieveMessageDetails.ExecuteReader
While dReader.Read
txtFrom.Text = dReader(3)
txtTo.Text = "This to be filled in"
txtSubject.Text = dReader(5)
txtDateSent.Text = dReader(4)
celMessageDetails.InnerHtml = dReader(6)
End While
dReader.Close()
SqlConnection1.Close()

If Request.QueryString("new") = "Yes" Then
Dim fileName As String
= "c:\inetpub\wwwroot\hbitl_umc\testInbox.xml"
Dim newStream As New System.IO.FileStream
(fileName, IO.FileMode.Append)
Dim xmlWriter As New System.Xml.XmlTextWriter
(newStream, System.Text.Encoding.ASCII)
dsMessage.WriteXml(xmlWriter)
xmlWriter.Close()
End If

My XML file turns to...

<?xml version="1.0" encoding="utf-8" ?>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mCreator>Empower Systems Demonstration
Site</mCreator>
<mPriority>1</mPriority>
<mSubject>Test Subject</mSubject>
<mDateReceived>07/08/2003
13:13:00</mDateReceived>
</message>
</inbox>
<inbox>
<message>
<mUID>1</mUID>
<mType>1</mType>
<mPriority>1</mPriority>
<mCreator>Demonstration Site</mCreator>
<mDateCreated>2003-08-
07T13:13:00.0000000+01:00</mDateCreated>
<mSubject>Test Subject</mSubject>
<mDetails>&lt;B&gt;This is a
&lt;I&gt;test&lt;/I&gt; message&lt;/B&gt;</mDetails>
</message>
</inbox>

As you can see, the <inbox> root element is duplicated!
The new message element, and it's children, need to be
placed inside the <inbox> parent.

Can someone shed some light as to what I'm doing wrong.

TIA

Nov 11 '05 #3

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

Similar topics

10
2889
by: Greg Hurlman | last post by:
I've got what I'm sure is a very simple problem. In an ASP page, I am trying to write out 4 fields from a recordset in succession: Response.Write rs("LastName") Response.Write rs("Suffix")...
1
2856
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
2
2366
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
0
1739
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is...
8
17862
by: Ben | last post by:
Hi all, Just wondering how to write (using document.write) to a table cell. I have table with 3 rows and 3 colums. I want to write from within the Javascript to say third column of a first row....
4
2931
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
11
16194
by: Vmusic | last post by:
Hi, I am trying to write out an array of string variables to Notepad. I can't get SendKeys to accept the string variable only literal quoted strings. I DO NOT want the hassle of writing to a...
4
4314
by: cbtechlists | last post by:
I have an ASP app that we've moved from a Windows 2000 to a Windows 2003 server (sql server 2000 to sql server 2005). The job runs fine on the old servers. Part of the app takes a recordset and...
0
5863
by: kuguy | last post by:
Hi all, I'm new to the forums, so I hope this isn't in the wrong place... I have that "Software caused connection abort: socket write error" exception error that i've never meet before. ...
8
7084
by: Mateusz Viste | last post by:
Hi, I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed> section. However, when I load my script, it...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
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...
0
7501
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...
0
5633
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,...
1
5056
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...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
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 ...
1
768
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.