如何序列化.NET对象到Azure的Blob存储,而无需使用临时文件?临时文件、对象、序列化、NET

由网友(奮鬥的青春)分享简介:我要存储一个.NET对象到Azure的Blob存储。I want to store a .NET object into Azure Blob Storage.目前我序列化到使用XML文件的TextWriter ( episodeList 是我想要序列化对象):Currently I serialize it i...

我要存储一个.NET对象到Azure的Blob存储。

I want to store a .NET object into Azure Blob Storage.

目前我序列化到使用XML文件的TextWriter episodeList 是我想要序列化对象):

Currently I serialize it into an XML file using TextWriter (episodeList is the object I want serialized):

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes Xmlattr = new XmlAttributes();
Xmlattr.XmlRoot = new XmlRootAttribute("EPISODES");
overrides.Add(typeof(List<EpisodeData>), Xmlattr);
XmlSerializer serializer = new XmlSerializer(typeof(List<EpisodeData>), overrides);
TextWriter textWriter = new StreamWriter(@"C:movie.xml");
serializer.Serialize(textWriter, episodeList);
textWriter.Close();

,然后上传文件到Blob存储:

and then upload the file into Blob Storage:

CloudBlobClient blobStorage = createOrGetReferenceOfBlobStorage(folderName);
string uniqueBlobName = string.Format("{0}/{1}", folderName, fileName);
CloudBlockBlob blob = clouBblockBlobPropertySetting(blobStorage, uniqueBlobName, ".txt");
using (StreamWriter writer = new StreamWriter(blob.OpenWrite()))
{
    writer.Write(content);
} 

是否有可能以某种方式跳过该临时文件,以便XML被直接上传到Azure的Blob存储?

Is it possible to somehow skip the temporary file so that the XML is directly uploaded into Azure Blob Storage?

推荐答案

您可以执行以下操作。创建一个的MemoryStream 实例,并使用 XmlSerializer.Serialize(流流)的对象序列化到内存流,那么倒带使用要寻求流开始()。然后调用 CloudBlob.UploadFromStream()流中的内容上传到BLOB。

You could do the following. Create a MemoryStream instance and use XmlSerializer.Serialize(Stream stream) to serialize the object into the memory stream, then "rewind" the stream to beginning using Seek(). Then you call CloudBlob.UploadFromStream() to upload the stream contents to the blob.

阅读全文

相关推荐

最新文章