CollapseXML is a great XML generator library for the .NET platform.

Instead of trying to convince you by words, let the following example speak for itself!

There are three parts to CollapseXML:

1. A template

You need to tell CollapseXML how you want your XML to look like.

Instead of writing XSD schemas or manually generating XML elements in recursive loops and what not, provide CollapseXML with a template like this one:

<?xml version="1.0" encoding="utf-8" ?>
<template xmlns:cx="">
      cx:foreach="customer in Customers"
      cx:filter="IsCoefficientPositive | customer.Coefficient"
      <desc>Order count: <cx:value source="customer.Orders.Count" />.</desc>
        cx:foreach="order in customer.Orders"
        cx:filter-date="IsBefore2011 | order.Timestamp">
        <Item cx:foreach="item in order.Items"
          <desc>(cx|item.Amount) (cx|item.Article.Unit) of (cx|item.Article.Name).</desc>

2. A data object

CollapseXML will take any POCO class and traverse its properties to required depth to find data values.

This specific example will use the sample model shown on the following picture:


We will use an instance of the Repository class as our data object.

With the template previously defined and the model on the picture we come to the third part, the CollapseXML itself.

3. Collapse running code

It’s as simple as this:

string template = File.ReadAllText("example.xml");
Repository dataObject = new Repository();

XDocument doc = new Collapse()
	.SetFilter("IsCoefficientPositive", a => a[0].Value<Double>() > 0)
	.SetFilter("IsBefore2011", a => a[0].Value<DateTime>().Year < 2011)
	.ExportToXml(dataObject, template);


Of course, you may not even need filters, so the code could even be shorter, stripped down to this:

Collapse.Export(dataObject, template).Save("out.xml");

That’s how easy it is.

Let’s see the results!

Here is the output XML document (for this sample, with randomly generated content):

<?xml version="1.0" encoding="utf-8"?>
  <customer name="Martha Wade">
    <desc>Order count: 2.</desc>
    <order date="29.01.2009. 04:37:19">
      <Item amount="1000" name="Milk" unit="ml">
        <desc>1000 ml of Milk.</desc>
      <Item amount="700" name="Bread" unit="g">
        <desc>700 g of Bread.</desc>
    <order date="10.01.2008. 22:41:09">
      <Item amount="4" name="Ball" unit="pcs">
        <desc>4 pcs of Ball.</desc>
  <customer name="John Smith">
    <desc>Order count: 1.</desc>
    <order date="19.10.2004. 21:33:22">
      <Item amount="2" name="Bubblegum" unit="pcs">
        <desc>2 pcs of Bubblegum.</desc>
      <Item amount="4" name="Notebook" unit="pcs">
        <desc>4 pcs of Notebook.</desc>


As you can see, it’s easy, short and functional.
Try it yourself or read more about it

Get it from NuGetDocumentationChangelog


We are very interested in your feedback – how you used the library, were you satisfied, did you encounter any problem?

Please contact us or leave a comment below. We appreciate your feedback.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>