june alex
june alex

Reputation: 294

How to create a XML FIle and upload to azure blob in C#

I created XML data using the C# XDocument library. And I tried to upload the XML data to an azure blob. Below is my code.

 using Microsoft.WindowsAzure.Storage;
 using Microsoft.WindowsAzure.Storage.Blob;
 using System.Xml.Linq;
 public CloudBlobClient blobClient {get;}

public AzureBlobServices(string storageAccountConnection)
{
 StorageAccountConnection = storageAccountConnection;
 storageAccount = CloudStorageAccount.Parse(StorageAccountConnection);
 blobClient = storageAccount.CreateCloudBlobClient();
}

     public async Task sampleFile()
     {
      XDocument doc = new XDocument( new XElement( "body", 
                                    new XElement( "level1", 
                                    new XElement( "level2", "text" ), 
                                    new XElement( "level2", "other text" ) ) ) );
            MemoryStream stream = new MemoryStream();
            doc.Save(stream);
            var container = blobClient.GetContainerReference("folder location");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("Data/xml/Data.csv");
            await blockBlob.UploadFromStreamAsync(stream);
}

But the file is uploaded but there is nothing in the file. What is wrong with code?

file is available now.after adding based on Mario Vernari answer
xmlStream.Position = 0; enter image description here

Upvotes: 0

Views: 2158

Answers (1)

Mario Vernari
Mario Vernari

Reputation: 7304

Try to reset the stream's position:

MemoryStream stream = new MemoryStream();
doc.Save(stream);
stream.Position = 0;

Upvotes: 3

Related Questions