Export (0) Print
Expand All

XStreamingElement Constructor (XName, Object)

Initializes a new instance of the XStreamingElement class with the specified name and content.

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

public XStreamingElement(
	XName name,
	Object content
)

Parameters

name
Type: System.Xml.Linq.XName

An XName that contains the element name.

content
Type: System.Object

The contents of the element.

This constructor creates a streaming element with the specified content and attributes.

There is an implicit conversion from string to XName. Typical use of this constructor is to specify a string as the parameter instead of creating a new XName.

Queries are not iterated until the XStreamingElement is serialized. This is in contrast to using queries for content for an XElement, where queries are iterated at the time of construction of the new XElement.

For details about the valid content that can be passed to this constructor, see Valid Content of XElement and XDocument Objects.

This example uses the following XML file, named Source.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<Root>
  <Child Key="01">
    <GrandChild>aaa</GrandChild>
  </Child>
  <Child Key="02">
    <GrandChild>bbb</GrandChild>
  </Child>
  <Child Key="03">
    <GrandChild>ccc</GrandChild>
  </Child>
</Root>

This example creates a custom axis method that uses ReadFrom. The example creates a new XML tree using XStreamingElement, specifying content with a query on the custom axis method. This transform has a small memory footprint regardless of the size of the source XML file.

Note that this technique can be used only with C#, not Visual Basic, as Visual Basic does not support the yield return construct.

NoteNote:

The following example uses the yield return construct of C#. Because there is no equivalent feature in Visual Basic 2008, this example is provided only in C#.

static IEnumerable<XElement> StreamRootChildDoc(string uri)
{
    using (XmlReader reader = XmlReader.Create(uri))
    {
        reader.MoveToContent();
        // Parse the file and display each of the nodes.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    if (reader.Name == "Child")
                    {
                        XElement el = XElement.ReadFrom(reader) as XElement;
                        if (el != null)
                            yield return el;
                    }
                    break;
            }
        }
    }
}

static void Main(string[] args)
{
    XStreamingElement newRoot = new XStreamingElement("NewRoot",
        from el in StreamRootChildDoc("Source.xml")
        where (int)el.Attribute("Key") > 1
        select el.Element("GrandChild")
    );

    Console.WriteLine(newRoot);
}

This example produces the following output.

<NewRoot>
  <GrandChild>bbb</GrandChild>
  <GrandChild>ccc</GrandChild>
</NewRoot>

One approach to processing a text file is to write an extension method that streams the text file a line at a time using the yield return construct. You then can write a LINQ query that processes the text file in a lazy deferred fashion. If you then use the XStreamingElement to stream output, you then can create a transform from the text file to XML that uses a minimal amount of memory, regardless of the size of the source text file.

The following text file, People.txt, is the source for this example.

#This is a comment
1,Tai,Yee,Writer
2,Nikolay,Grachev,Programmer
3,David,Wright,Inventor

The following code contains an extension method that streams the lines of the text file in a deferred fashion.

NoteNote:

The following example uses the yield return construct of C#. Because there is no equivalent feature in Visual Basic 2008, this example is provided only in C#.

public static class StreamReaderExtension
{
    public static IEnumerable<string> Lines(this StreamReader source)
    {
        String line;
        if (source == null)
            throw new ArgumentNullException("source");
        while ((line = source.ReadLine()) != null)
            yield return line;
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (StreamReader sr = new StreamReader("People.txt"))
        {
            XStreamingElement xmlTree = new XStreamingElement("Root",
                from line in sr.Lines()
                let items = line.Split(',')
                where !line.StartsWith("#")
                select new XElement("Person",
                           new XAttribute("ID", items[0]),
                           new XElement("First", items[1]),
                           new XElement("Last", items[2]),
                           new XElement("Occupation", items[3])
                       )
            );
            Console.WriteLine(xmlTree);
        }
    }
}

This example produces the following output:

<Root>
  <Person ID="1">
    <First>Tai</First>
    <Last>Yee</Last>
    <Occupation>Writer</Occupation>
  </Person>
  <Person ID="2">
    <First>Nikolay</First>
    <Last>Grachev</Last>
    <Occupation>Programmer</Occupation>
  </Person>
  <Person ID="3">
    <First>David</First>
    <Last>Wright</Last>
    <Occupation>Inventor</Occupation>
  </Person>
</Root>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft