0

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

3
  • What's wrong with that XML string, apart from using Excel to open a text file? XML is text. Post the file contents as text and explain what you actually want. What you posted is exactly what your code produces Commented Aug 30, 2021 at 10:30
  • 1
    Data/xml/Data.csv ? CSV is not XML. CSV is just a flat file with commas between values, an optional header and no other structure, eg 1,2,3.14,4.5. Commented Aug 30, 2021 at 10:34
  • When you double-click on a file with the csv extension Excel will try to open the file as if it was a CSV file - fields separated by commas, rows by newlines. In this case there are no commas, just newlines, so Excel loaded the file as single-column text. Commented Aug 30, 2021 at 11:26

1 Answer 1

3

Try to reset the stream's position:

MemoryStream stream = new MemoryStream();
doc.Save(stream);
stream.Position = 0;
Sign up to request clarification or add additional context in comments.

5 Comments

Content is now available in the file. but not in correct format separated. attached is the Screen shot in question.
@junealex what do you mean "not in correct format"? What did you expect? That screenshot is exactly what your code produces. Are you trying to create an Excel file instead? You can't do it this way
@Panagiotis Kanavos after I download the file and tried to open in MS EXCEL application but its showing as it in Screen shot
Why did you use Excel to open an XML file in the first place? What are you trying to generate? \
I am tring to create a CSV file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.