[英]How do I serialize a .NET object into Azure Blob Storage without using a temporary file?
I want to store a .NET object into Azure Blob Storage. 我想将.NET对象存储到Azure Blob存储中。
Currently I serialize it into an XML file using TextWriter
( episodeList
is the object I want serialized): 目前我使用
TextWriter
将其序列化为XML文件( episodeList
是我想要序列化的对象):
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();
and then upload the file into Blob Storage: 然后将文件上传到Blob存储:
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);
}
Is it possible to somehow skip the temporary file so that the XML is directly uploaded into Azure Blob Storage? 有可能以某种方式跳过临时文件,以便将XML直接上载到Azure 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()
. 创建一个
MemoryStream
实例并使用XmlSerializer.Serialize(Stream stream)
将对象序列化到内存流中,然后使用Seek()
将流“回放”到开头。 Then you call CloudBlob.UploadFromStream()
to upload the stream contents to the blob. 然后调用
CloudBlob.UploadFromStream()
将流内容上传到blob。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.