的一部分,我想寫一個非常大的XML文檔,所以System.Xml.Linq.XStreamingDocument
似乎是理想的解決方案。XStreamingElement作爲一個XDocument
我想我可以構造XDocument
來包含XStreamingDocument
,並且只有當我呼叫Save()
時樹會被遍歷並且可枚舉被迭代。
public void WriteXml(IEnumerable<string> lotsOfStrings, TextWriter output)
{
var rootElement = new XElement("Root",
new XStreamingElement ("Strings", lotsOfStrings.Select(s => new XElement("Line", s))));
var document = new XDocument(rootElement);
document.Save(output);
}
但事實並非如此。只要我將XStreamingElement
添加到包含元素中,它就會遍歷枚舉並生成所有元素。
我打算嵌套幾個要素深這麼久元素列表。在致電XStreamingElement.WriteTo()
之前,是否必須使用XmlWriter
手動編寫包含結構?這將是有點乏味,並最終與我混合範例(XmlWriter
和System.Xml.Linq
)。這似乎確實限制了XStreamingElement
的有用性。
還是我失去了構建一個包含流式傳輸內容的文件的一些方法?
當您將它添加到XDocument中時,如何知道它遍歷元素?如果您在調試時顯示了它的值,那麼會導致'.ToString()'被元素迭代調用。 – xjuice
@xjuice我在'Select' lambda中放置了一個斷點。 'document.Save()'被擊中之前碰到了。也可以使用具有副作用的迭代器進行驗證。 –