The XStreamingElement class provides a way to lazily construct XML trees. It is useful when you want to defer the creation of child nodes until they are needed.
System.Xml.Linq
System.Xml.Linq.dll
Use this class to construct XML elements in a streaming fashion. It inherits from XContainer.
public class XStreamingElement : XContainer
public XStreamingElement(XName name);
public XStreamingElement(XName name, params object[] content);
public XStreamingElement(string name);
public XStreamingElement(string name, params object[] content);
| Member | Description |
|---|---|
public XName Name { get; } | Gets the name of the element. |
public IEnumerable | Returns a collection of the child nodes. |
public override void WriteTo(XmlWriter writer) | Writes this element to an XmlWriter. |
using System;
using System.Xml.Linq;
class Program
{
static void Main()
{
var streaming = new XStreamingElement("Items",
from i in Enumerable.Range(1, 5)
select new XElement("Item",
new XAttribute("Id", i),
new XElement("Value", $"Value {i}")));
Console.WriteLine(streaming);
}
}
Output:
<Items>
<Item Id="1">
<Value>Value 1</Value>
</Item>
<Item Id="2">
<Value>Value 2</Value>
</Item>
<Item Id="3">
<Value>Value 3</Value>
</Item>
<Item Id="4">
<Value>Value 4</Value>
</Item>
<Item Id="5">
<Value>Value 5</Value>
</Item>
</Items>