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 / May 2005

Tip: Looking for answers? Try searching our database.

Appending Node to Large XML File

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonathan Taylor - 20 May 2005 13:17 GMT
I have a large XML file, that is too large to read in to XmlDocument.

I need to append data to this XML file without creating a new file, since I
don't want to have two copies of the large file on the server.

I've not seen any example that works so far, even with google.  Can anyone
help ?
Yingzi Le - 20 May 2005 23:21 GMT
Hi Jonathan,

You can use XmlTextReader and XmlTextWriter. They're very fast. You can
programmatically delete the old file if you don't want to keep two large
files.

To start you can read through some documentations here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlxmltextwriterclasstopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlxmltextreaderclassnodetypetopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlxmltextwriterclasstopic.asp

The following are some sample code:

//reader the Large Xml file using XmlTextReader
XmlTextReader reader = new XmlTextReader(@"C:\Test\LargeXmlFile.xml");

//XmlTextWriter to write Large XML File
XmlTextWriter cdocWriter =  new
XmlTextWriter(@"C:\Test\LargeXmlFile_New.xml", Encoding.UTF8);

writer.Formatting = Formatting.Indented;

writer.WriteStartDocument();

// Read the file and display each of the nodes.
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element:
            writer.WriteStartElement(reader.Name);
            writer.WriteAttributes(reader,false);
            if (reader.IsEmptyElement)
                writer.WriteEndElement();
                break;
            //You can have some test statement here to append nodes
            if (reader.Name == "????")
            {
                //TODO: do something
            }
        case XmlNodeType.Text:
            writer.WriteString(reader.Value);
                break;
        case XmlNodeType.Whitespace:
        case XmlNodeType.SignificantWhitespace:
                writer.WriteWhitespace(reader.Value);
                break;
        case XmlNodeType.CDATA:
                writer.WriteCData(reader.Value);
                break;
        case XmlNodeType.Comment:
                writer.WriteComment(reader.Value);
                break;
        case XmlNodeType.EntityReference:
                writer.WriteEntityRef(reader.Name);
                break;
        case XmlNodeType.EndElement:
            //You can have some test statement here to append nodes
            if (reader.Name == "????")
            {
                //TODO: do something
            }

                writer.WriteEndElement();
                break;
        }
}

writer.WriteEndDocument();
writer.Flush();
writer.Close();
reader.Close();

Hope this helps!
--
Yingzi Le
LongHorn SDK Team

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "Jonathan Taylor" <jontay@00000000homtail.com>
>Subject: Appending Node to Large XML File
[quoted text clipped - 8 lines]
>Newsgroups: microsoft.public.dotnet.xml
>NNTP-Posting-Host: 82-41-12-138.cable.ubr02.edin.blueyonder.co.uk
82.41.12.138
>Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
>Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.xml:7123
[quoted text clipped - 7 lines]
>I've not seen any example that works so far, even with google.  Can anyone
>help ?

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.