Search code examples
c#readxml

Reading a large 1 GB xml file


I need open a large XML file and append an AddressInfo element into existing file. What is the best and fastest way to do this?

My XML example:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAddressInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AddressInfo>    
    <Level1></Level1>
    <Level2>2010-10-29T19:53:32</Level2>
    <Level3>/Level3>
    <Level4></Level4>    
  </AddressInfo>
   <AddressInfo>    
    <Level1></Level1>
    <Level2>2010-10-29T19:53:32</Level2>
    <Level3>/Level3>
    <Level4></Level4>    
  </AddressInfo>
</ArrayOfAddressInfo>

Solution

  • Something like this

            string lastTag = "</ArrayOfAddressInfo>";
            string newNode = "\r\n<AddressInfo>\r\n<Level1/>\r\n</AddressInfo>";
            int offset = 5;
            using (FileStream xmlstream = new FileStream(
                @"test.xml",
                FileMode.Open,
                FileAccess.ReadWrite,
                FileShare.None))
            {
    
                // Get to the appx position, assumes the last tag is the
                // last thing in the file.  Adjust the offset accordingly
                // for your needs
                xmlstream.Seek(-(lastTag.Length + offset), SeekOrigin.End);
    
                // Check - are we at the >
                while (xmlstream.ReadByte() != '>')
                    ;
    
                // Write our bit of xml
                xmlstream.Write(
                    Encoding.ASCII.GetBytes(newNode),
                    0, newNode.Length);
    
                // Rewrite the last tag
                xmlstream.Write(
                    Encoding.ASCII.GetBytes("\r\n" + lastTag + "\r\n"),
                    0, lastTag.Length + 2);
                xmlstream.Close();
            }