XStreamingElement Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Represents elements in an XML tree that supports deferred streaming output.
Inheritance Hierarchy
System.Object
System.Xml.Linq.XStreamingElement
Namespace: System.Xml.Linq
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
Syntax
'Declaration
Public Class XStreamingElement
public class XStreamingElement
The XStreamingElement type exposes the following members.
Constructors
Name | Description | |
---|---|---|
XStreamingElement(XName) | Initializes a new instance of the XElement class from the specified XName. | |
XStreamingElement(XName, Object) | Initializes a new instance of the XStreamingElement class with the specified name and content. | |
XStreamingElement(XName, array<Object[]) | Initializes a new instance of the XStreamingElement class with the specified name and content. |
Top
Methods
Name | Description | |
---|---|---|
Add(Object) | Adds the specified content as children to this XStreamingElement. | |
Add(array<Object[]) | Adds the specified content as children to this XStreamingElement. | |
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Save(Stream) | Outputs this XStreamingElement to the specified Stream. | |
Save(TextWriter) | Serialize this streaming element to a TextWriter. | |
Save(XmlWriter) | Serialize this streaming element to an XmlWriter. | |
Save(Stream, SaveOptions) | Outputs this XStreamingElement to the specified Stream, optionally specifying formatting behavior. | |
Save(TextWriter, SaveOptions) | Serialize this streaming element to a TextWriter, optionally disabling formatting. | |
ToString() | Returns the formatted (indented) XML for this streaming element. (Overrides Object.ToString().) | |
ToString(SaveOptions) | Returns the XML for this streaming element, optionally disabling formatting. | |
WriteTo | Writes this streaming element to an XmlWriter. |
Top
Remarks
This class allows you to create an XML tree that supports deferred streaming output. You use this class to create an XML tree in a very similar fashion to creating an XML tree using XElement. However, there is a fundamental difference. When you use a LINQ query to specify content when creating an XML tree using XElement, the query variable is iterated at the time of construction of the XML tree, and the results of the query are added to the XML tree. In contrast, when you create an XML tree using XStreamingElement, a reference to the query variable is stored in the XML tree without being iterated. Queries are iterated only upon serialization. This allows you to create larger XML trees while maintaining a smaller memory footprint.
If you are streaming from an input source, such as a text file, then you can read a very large text file, and generate a very large XML document while maintaining a small memory footprint.
Another scenario is that you have a large XML tree that has been loaded into memory, and you want to create a transformed version of the document. If you create a new document using XElement, then you will have two large XML trees in memory upon completion of the transformation. However, if you create the new XML tree using XStreamingElement, then your working set will be effectively cut in half.
Note that when debugging a program that uses XStreamingElement, displaying the value of an object causes its ToString method to be called. This causes the XML to be serialized. If the semantics of your streaming element query are such that the streaming element can only be streamed once, this may cause undesirable behavior in your debugging experience.
Examples
The following example first creates a source XML tree. It then creates a transform of the source XML tree using XElement. This transform creates a new tree in memory. It then creates a transform of the source XML tree using XStreamingElement. This transform doesn't execute the query until the transformed tree is serialized to the console. Its memory usage is less.
Dim output As New StringBuilder
Dim srcTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
</Root>
Dim dstTree1 As XElement = _
<NewRoot>
<%= From el In srcTree.Elements _
Where (el.Value >= 3) _
Select <DifferentChild><%= el.Value %></DifferentChild> %>
</NewRoot>
Dim dstTree2 As XStreamingElement = New XStreamingElement("NewRoot", _
From el In srcTree.Elements _
Where el.Value >= 3 _
Select <DifferentChild><%= el.Value %></DifferentChild> _
)
output.Append(dstTree1)
output.Append(Environment.NewLine)
output.Append("------")
output.Append(Environment.NewLine)
output.Append(dstTree2)
output.Append(Environment.NewLine)
OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
XElement srcTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5)
);
XElement dstTree1 = new XElement("NewRoot",
from el in srcTree.Elements()
where (int)el >= 3
select new XElement("DifferentChild", (int)el)
);
XStreamingElement dstTree2 = new XStreamingElement("NewRoot",
from el in srcTree.Elements()
where (int)el >= 3
select new XElement("DifferentChild", (int)el)
);
output.Append(dstTree1 + Environment.NewLine);
output.Append("------" + Environment.NewLine);
output.Append(dstTree2 + Environment.NewLine);
OutputTextBlock.Text = output.ToString();
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also