Export (0) Print
Expand All

XStreamingElement Class

Represents elements in an XML tree that supports deferred streaming output.

System.Object
  System.Xml.Linq.XStreamingElement

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public class XStreamingElement

The XStreamingElement type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360XStreamingElement(XName)Initializes a new instance of the XElement class from the specified XName.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360XStreamingElement(XName, Object)Initializes a new instance of the XStreamingElement class with the specified name and content.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360XStreamingElement(XName, Object[])Initializes a new instance of the XStreamingElement class with the specified name and content.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NameGets or sets the name of this streaming element.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Add(Object)Adds the specified content as children to this XStreamingElement.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Add(Object[])Adds the specified content as children to this XStreamingElement.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Save(Stream)Outputs this XStreamingElement to the specified Stream.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Save(TextWriter)Serialize this streaming element to a TextWriter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Save(XmlWriter)Serialize this streaming element to an XmlWriter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Save(Stream, SaveOptions)Outputs this XStreamingElement to the specified Stream, optionally specifying formatting behavior.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Save(TextWriter, SaveOptions)Serialize this streaming element to a TextWriter, optionally disabling formatting.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToString()Returns the formatted (indented) XML for this streaming element. (Overrides Object.ToString().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToString(SaveOptions)Returns the XML for this streaming element, optionally disabling formatting.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WriteToWrites this streaming element to an XmlWriter.
Top

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.


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();


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

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft