Created
July 18, 2013 15:51
-
-
Save mfcollins3/6030475 to your computer and use it in GitHub Desktop.
Example that uses the TPL Dataflow library to process XML records and output a formatted string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Threading.Tasks.Dataflow; | |
using System.Xml.Linq; | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
var inputBlock = new BufferBlock<string>(); | |
var transformInputBlock = new TransformBlock<string, XDocument>(s => XDocument.Parse(s)); | |
var processBlock = new TransformBlock<XDocument, Tuple<string, int>>( | |
x => | |
{ | |
var person = x.Element("person"); | |
return Tuple.Create((string)person.Element("name"), (int)person.Element("age")); | |
}); | |
var transformOutputBlock = | |
new TransformBlock<Tuple<string, int>, string>( | |
t => string.Format(CultureInfo.CurrentCulture, "{0} is {1} years old", t.Item1, t.Item2)); | |
var outputBlock = new ActionBlock<string>(m => Console.Out.WriteLine(m)); | |
using (inputBlock.LinkTo(transformInputBlock)) | |
using (transformInputBlock.LinkTo(processBlock)) | |
using (processBlock.LinkTo(transformOutputBlock)) | |
using (transformOutputBlock.LinkTo(outputBlock)) | |
{ | |
inputBlock.Completion.ContinueWith(t => transformInputBlock.Complete()); | |
transformInputBlock.Completion.ContinueWith(t => processBlock.Complete()); | |
processBlock.Completion.ContinueWith(t => transformOutputBlock.Complete()); | |
transformOutputBlock.Completion.ContinueWith(t => outputBlock.Complete()); | |
var records = new[] | |
{ | |
"<person><name>Michael Collins</name><age>38</age></person>", | |
"<person><name>George Washington</name><age>281</age></person>", | |
"<person><name>Abraham Lincoln</name><age>204</age></person>" | |
}; | |
foreach (var record in records) | |
{ | |
inputBlock.Post(record); | |
} | |
inputBlock.Complete(); | |
outputBlock.Completion.Wait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment