By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Write XML

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.