title: Azure - Updating an existing xml file in BLOB storage
body: <p>I have XML files stored in BLOB storage, and I am trying to figure out what is the most efficient way to update them ( and/or add some elements to them). In a WebRole, I came up with this :  </p>
  
  <pre class="lang-cs prettyprint-override"><code>using (MemoryStream ms = new MemoryStream())
  {                    
        var blob = container.GetBlobReference("file.xml");
        blob.DownloadToStream(msOriginal);
        XDocument xDoc= XDocument.Load(ms);  
  
        // Do some updates/inserts using LINQ to XML.  
  
        blob.Delete();//Details about this later on.
  
        using(MemoryStream msNew = new MemoryStream())  
        {
             xDoc.Save(msNew);
             msNew.Seek(0,SeekOrigin.Begin);
             blob.UploadFromStream(msNew);                    
        }                               
  }
  </code></pre>
  
  <p>I am looking at these parameters considering the efficiency:  </p>
  
  <ol>
  <li>BLOB <strong>Transactions</strong>.</li>
  <li><strong>Bandwidth</strong>. (Not sure if it's counted, because the code runs in the data-center)</li>
  <li><strong>Memory</strong> consumption on the instance.</li>
  </ol>
  
  <p>Some things to mention:</p>
  
  <ul>
  <li><p>My xml files are around 150-200 KB.</p></li>
  <li><p>I am aware of the fact that XDocument loads the whole file into
  memory, and working in streams ( XmlWriter and XmlReader ) could
  solve this. But I Assume this will require working with BlobStream
  which could lead to less efficient transaction-wise (I think).</p></li>
  <li><p>About blob.Delete(), without it, the uploaded xml in the blob storage
  seems to be missing some closing tags at the end of it. I assumed
  this is caused by a collision with the old data. I could be
  completely wrong here, but using the delete solved it ( costing one
  more transaction though ).</p></li>
  </ul>
  
  <p>Is the code I provided is a good practice or maybe a more efficient way exists considering the parameters I mentioned ? </p>