Click here to Skip to main content
15,942,756 members
Articles / Programming Languages / C#

Conditional Replacement

Rate me:
Please Sign up or sign in to vote.
4.09/5 (6 votes)
20 Sep 20053 min read 35.7K   375   19   3
Templating for text based formats.

Sample Image


Conditional Replacement is used to merge data in an object model with text based templates.

It uses Reflection to read the properties and methods of your data object and recursively merges the data with the template using conditional placeholders, e.g., Welcome [Name] to the Code Project. Here the Name place holder would be replaced with the data in your object on its 'Name' property.


Most IT projects require the production of a formatted version of the data the system is manipulating, whether it be CSV/Excel/Word documents for reports and invoicing, XML for web services, HTML pages for a content management system, etc.

CRep was developed to enable the creation of a variety of text based documents in a structured, re-usable and simple to read manner.

The Demo Project

This contains a simple object model for the RSS feed available on this site. It loads the data into the object model and generates an HTML file from the data and the template provided. You will require the .NET framework 1.1.

Using the code

When creating a ConditionalReplacement object you are required to pass in the template as a string.

You will then be able to generate your result by calling the Replace function with your data object.

// Conditional Replacement Object
// Initialized with the template
ConditionalReplacement cRep = 
  new ConditionalReplacement( "Welcome [Name] to the Code Project" );

// Replace and write to output file
string output = cRep.Replace( UserDataObject );

Syntax for Templates

CRep looks for placeholders in the template, these are marked using the following tokens:

[Open place holder
]Close place holder
|General Separator
@Collection Indicator
#Format indicator
\Token Switch


Simple replacement of placeholders with data; you can access sub object properties using the standard 'dot' notation.


E.g. Hello [Person.Forename]

-> Hello Anthony

*N.B. If you find you need to use a token in the template and want CRep to ignore it, just apply the switch '\' before it.

E.g. Hello \[[Person.Forename]\]

-> Hello [Anthony]


Replacement of placeholders based on the data in the property; you can have as many conditions as you require.


E.g. Hello [Person.Forname] ([Person.Forname|=Anthony|great|~|nice] name)

If the person's forename is 'Anthony':

-> Hello Anthony (great name)


-> Hello Sarah (nice name)

Recurse Sub-Object

You can recurse to sub objects. Think of it as an embedded template using the sub object as its data.


E.g. Hello [Person|[Forname] [Surname]]

-> Hello Anthony Johnston

Recurse Sub-Collection

You can recurse the objects in sub collections too. Each object will be subject to the 'embedded template'.


E.g. Hello [People@[Person.Forname], ]

-> Hello Anthony, Sarah,

Formatting Data

Data can be formatted, this uses the standard String.Format() method. More details on this method can be found on the Microsoft web site.


E.g. Your birth date was [Person.DateOfBirth#d MMM yyyy]

-> Your birth date was 1 Dec 1970

Regular Expressions

Some may ask why it doesn't use regular expressions, there are three compelling reasons;

  1. Regular expressions don't handle nested placeholders.
  2. Regular expressions are difficult to read and understand.
  3. Regular expressions are slow.


  • 1.0 - 6 September 2005.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralGood work Pin
Magomed Abdurakhmanov18-Sep-08 9:35
Magomed Abdurakhmanov18-Sep-08 9:35 
GeneralFile naming Pin
fwsouthern20-Sep-05 18:08
fwsouthern20-Sep-05 18:08 
GeneralRe: File naming Pin
Anthony Johnston28-Sep-05 10:42
Anthony Johnston28-Sep-05 10:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.