473,394 Members | 1,165 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,394 software developers and data experts.

Q/VB.NET: Append data to XML file without loading complete file?

Can someone give me an example how to append data without having to
load the complete XML file.

Is this possible?

Nov 12 '05 #1
3 23908
"Jonathan Buckland" <jo******@theinformationpeople.com> wrote in message news:94**************************@posting.google.c om...
Can someone give me an example how to append data without having to
load the complete XML file.

The ability to append data to an XML file without processing the entire
document greatly depends on the structure of the XML, and the place
in the file where you want to append.

As a simple example to get started with, consider the following relatively
flat XML instance document that contains the records of several fictitious
healthcare providers:

- - - Providers.xml (before)
<?xml version="1.0" encoding="iso-8859-1" ?>
<Provider specialty="Endocrinologist" id="NYC00001">
<Name>Dr. No</Name>
<Phone kindOf="pager">212-555-9876</Phone>
<Provider specialty="GeneralPractitioner" id="PHL00001">
<Name>Dr. Who</Name>
<Phone kindOf="mobile">215-555-4567</Phone>
- - -

What do we know about this file? It's ISO-8859-1 ANSI text,
one byte to a character. If what we need to do is append another
provider to this file, then these additional records can be inserted
immediate before the </Providers> closing tag. Counting the new-
line (CR and LF control codes) after </Providers>, and one byte
per character of the text representation, "</Providers>" we know
that this insert position is 12 + 2 (vbCrLf) = 14 bytes before the
end of the file.

Hmmm... now this is a text file in the file system, so in addition to
writing to it with an XmlWriter object (which is helpful when emitting
well-formed XML, that is, XML nodes with matching start and end
tags) we can also write to it using a conventional StreamWriter from
the System.IO namespace.

This analysis lends itself to the following example VB.NET code,

- - - Append.vb
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Xml

Public Class AppendXmlSample

Public Shared Sub Main()

' Open the XML document as an ordinary text file.
Dim fileOut As New FileStream( "Providers.xml", _
FileMode.Open, FileAccess.Write)

' Move back 14 bytes from the end of the stream and write away.
fileOut.Seek( -14, SeekOrigin.End)

' Wrap the file output stream in a StreamWriter, I name it closer
' because you will see I'll need it to finish off the XML document.
Dim closer As New StreamWriter( fileOut)

' Wrap the stream writer in an XmlTextWriter, which I use to emit
' the well-formed XML records being appended into the file, before
' the document element's closing tag.
Dim writer As New XmlTextWriter( closer)

' Pretty-print the XML output.
writer.Formatting = Formatting.Indented
writer.IndentChar = vbTab
writer.Indentation = 1

' This next block of code just emits a well-formed XML fragment
' representing a fictitious physician.
writer.WriteStartElement( String.Empty, "Provider", String.Empty)
writer.WriteStartAttribute( String.Empty, "specialty", String.Empty)
writer.WriteString( "Cardiologist")
writer.WriteStartAttribute( String.Empty, "id", String.Empty)
writer.WriteString( "LAX00001")
writer.WriteStartElement( String.Empty, "Name", String.Empty)
writer.WriteString( "Dr. Love")
writer.WriteStartElement( String.Empty, "Phone", String.Empty)
writer.WriteStartAttribute( String.Empty, "kindOf", String.Empty)
writer.WriteString( "home")
writer.WriteString( "310-555-1234")

' Flush the XML content to the file, because at this point I am
' done with the XmlTextWriter. I don't Close because I don't
' want to close the file output stream quite yet.

' I've already overwritten the document end tag, and I can't emit
' this end tag with XmlTextWriter because its unbalanced (I did
' not write the document start tag with the XmlTextWriter).
' What I do is just emit the text representation of the end tag,
' angle brackets and all.
closer.Write( vbCrLf + "</Providers>" + vbCrLf)

' Now I am done, I can close.

writer = Nothing
closer = Nothing
fileOut = Nothing

End Sub

End Class
- - -

If you build the AppendXmlSample like this,

vbc Append.vb /r:System.Xml.dll

and then run the resulting Append executable from the command-line
in the directory containing a copy of Providers.xml, the resulting XML
file afterwards will look like the following with the additional record for
Dr. Love:

- - - Providers.xml (after)
<?xml version="1.0" encoding="iso-8859-1" ?>
<Provider specialty="Endocrinologist" id="NYC00001">
<Name>Dr. No</Name>
<Phone kindOf="pager">212-555-9876</Phone>
<Provider specialty="GeneralPractitioner" id="PHL00001">
<Name>Dr. Who</Name>
<Phone kindOf="mobile">215-555-4567</Phone>
<Provider specialty="Cardiologist" id="LAX00001">
<Name>Dr. Love</Name>
<Phone kindOf="home">310-555-1234</Phone>
- - -

The flat XML instance document described above illustrates one way
of appending content to the end of the file, in a manner that preserves
the well-formedness of the document. More complex schemas tend
to prohibit taking this approach where the requirement is to append
child nodes that are deeply nested.

A more sophisticated technique (perhaps too low-level for VB.NET)
would involve analyzing the NTFS file system and the arrangement of
physical sectors within the file system. It's conceivable you could edit
the sector containing the piece of serialized XML at which you want to
append a deeply nested child node. The sector could be quickly located
if you maintained an index mapping sectors to locations within the XML
document. Next you would append the data within the file, and relocate
the disturbed data following it to another sector (or sector(s)), fixing-up
the allocation table to maintain the appropriate linkages between sectors.
XML content relocated to other sectors could have insignificant white-
space injected within it, to re-establish it as being an even multiple of
the sector size.

For extremely large and complex XML files with the requirement to
append low-level nodes, this second technique is usually necessary
to achieve expeditious updates. It can be seen in some XML DB
(or "accelerator") implementations, but it is not a solution for the
Derek Harmon
Nov 12 '05 #2

Hi Jonathan,

We have reviewed your post, and will do some research on this issue.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 12 '05 #3
Hi Jonathan,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to process the XML file
without reading the whole document to the memory. If there is any
misunderstanding, please feel free to let me know.

As far as I know, this cannot be done with DOM, due to the DOM's tree-based
model, most implementations demand that the entire XML document be
contained in memory while processing. So we have to achieve this using SAX.

SAX stands for the Simple API for XML. SAX models the Infoset through a
linear sequence of well-known method calls. Because SAX doesn't demand
resources for an in-memory representation of the document, it's a
lightweight alternative to the DOM. It is implemented in MSXML.

You can find many resources if you search in MSDN with the keyword SAX.
Here I have listed some of them. Hope this helps:


Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
"This posting is provided "AS IS" with no warranties, and confers no

Nov 12 '05 #4

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

Similar topics

by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
by: Matt | last post by:
I have a requirement where a user upload a CSV file to the server through an ASP.NET page. After the file is uploaded the ASP.NET page then has to upload the contents of this file into an Oracle...
by: Hernan Garber | last post by:
Hi, Attached, the installation log. Win 2k Server SP3 IE6SP1/SQL Server Installed Thanks Setup.exe: Setup.exe: ========== Logging started ==========
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
by: phnimx | last post by:
Hi , We have developed a number of plug-in .NET Library Components that we typically deploy with our various applications by installing them into the GAC. Each of the applications contains an...
by: shamirza | last post by:
4 9 6 18.ATLAS-AJAX Note: - As an IT professional it's useful to know what the difference is between Hype and usefulness. For instance if there is a new technology coming in many programmers...
by: shamirza | last post by:
When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and...
by: shamirza | last post by:
What is view state and use of it? The current property settings of an ASP.NET page and those of any ASP.NET server controls contained within the page. ASP.NET can detect when a form is requested...
by: sylvaticus | last post by:
Hello, is it possible to append some data on a text output file appending the data to each row, and without use too much computational I/O ?? Instead of the classical way: VARX VARY ...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
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,...
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...

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.