次の方法で共有


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
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XStreamingElement(XName) Initializes a new instance of the XElement class from the specified XName.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XStreamingElement(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 360 XStreamingElement(XName, array<Object[]) Initializes a new instance of the XStreamingElement class with the specified name and content.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Name Gets or sets the name of this streaming element.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Add(Object) Adds the specified content as children to this XStreamingElement.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Add(array<Object[]) Adds the specified content as children to this XStreamingElement.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Save(Stream) Outputs this XStreamingElement to the specified Stream.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Save(TextWriter) Serialize this streaming element to a TextWriter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Save(XmlWriter) Serialize this streaming element to an XmlWriter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Save(Stream, SaveOptions) Outputs this XStreamingElement to the specified Stream, optionally specifying formatting behavior.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Save(TextWriter, SaveOptions) Serialize this streaming element to a TextWriter, optionally disabling formatting.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString() Returns the formatted (indented) XML for this streaming element. (Overrides Object.ToString().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(SaveOptions) Returns the XML for this streaming element, optionally disabling formatting.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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.