Database Reference
In-Depth Information
}
}
Processing XML with XmlReader and LINQ to XML
This pattern makes use of the XmlReader class to stream in an XML document, as
well as LINQ to XML functionality to extract the values you want to keep. It is ideal
for processing large XML documents, since it does not require the entire document to
be read into memory. It is also well suited for scenarios where you want to extract cer-
tain fields from the XML document and ignore the rest.
Note The idea for this pattern came from a post from SQL Server MVP, Simon
Sabin. Sample code and other great SSIS content can be found on his blog at ht-
tp://sqlblogcasts.com/blogs/simons/ .
The key to this pattern is the use of the XmlReader class. Instead of using the
XDocument class to read our source XML file (which is the typical approach when
using LINQ to XML), we'll create a special function that returns the XML as a collec-
tion of XElements. This allows us to make use of the LINQ syntax while taking ad-
vantage of the streaming functionality provided by XmlReader .
Before adding the code, you'll need to add the following namespaces to your us-
ing statements:
System.Collections.Generic
System.Linq
System.Xml.Linq
Listing 9-9 contains the code for the XmlReader function ( StreamReader ), as
well as the CreateNewOutputRows logic to consume the XML document.
Listing 9-9 . Script Logic for Using the XmlReader Class
public override void CreateNewOutputRows()
{
foreach (var xdata in (
 
 
Search WWH ::




Custom Search