流XML序列化在.NET序列化、XML、NET

2023-09-03 00:41:26 作者:捧一束彼岸花

我试图序列化一个非常大的的IEnumerable<为MyObject>使用的XmlSerializer 不保持所有的对象在存储器中。

I'm trying to serialize a very large IEnumerable<MyObject> using an XmlSerializer without keeping all the objects in memory.

的IEnumerable&LT;为MyObject&GT; 其实是懒惰的。

我正在寻找一个流媒体解决方案,将:

I'm looking for a streaming solution that will:

在以一个对象从的IEnumerable&LT;为MyObject&GT; 它序列化到使用标准的序列化基础流(我不想在这里手制的XML!的) 弃内存中的数据,并移动到下一 Take an object from the IEnumerable<MyObject> Serialize it to the underlying stream using the standard serialization (I don't want to handcraft the XML here!) Discard the in memory data and move to the next

我想这个code:

using (var writer = new StreamWriter(filePath))
{
 var xmlSerializer = new XmlSerializer(typeof(MyObject));
  foreach (var myObject in myObjectsIEnumerable)
  {
   xmlSerializer.Serialize(writer, myObject);
  }
}

但我发现了多个XML头,我不能指定一个根标签&LT; MyObjects&GT; 所以我的XML是无效的。

but I'm getting multiple XML headers and I cannot specify a root tag <MyObjects> so my XML is invalid.

你知道吗?

感谢

推荐答案

的XmlTextWriter 类是XML生成一个快速流API。这是相当低的水平,MSDN有实例化一个确证的文章 XmlTextWriter中使用 XmlWriter.Create()

The XmlTextWriter class is a fast streaming API for XML generation. It is rather low-level, MSDN has an article on instantiating a validating XmlTextWriter using XmlWriter.Create().

 
精彩推荐
图片推荐