XStreamingElement Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Represents elements in an XML tree that supports deferred streaming output.
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
The XStreamingElement type exposes the following members.
| 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. |
| 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. |
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.
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.


