This is a pretty typical reason to use XSLT, which is an efficient and powerful tool for transforming one XML document into another (or into HTML, or text).
Here's a minimal program to perform an XSLT transform and send the results to the console:
using System;
using System.Xml;
using System.Xml.Xsl;
namespace XsltTest
{
class Program
{
static void Main(string[] args)
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("test.xslt");
XmlWriter xw = XmlWriter.Create(Console.Out);
xslt.Transform("input.xml", xw);
xw.Flush();
xw.Close();
Console.ReadKey();
}
}
}
Here's the actual XSLT, which is saved in test.xslt
in the program directory. It's pretty simple: given an input document whose top-level element is named input
, it creates an output
element and copied over every child element whose value
attribute is set to true
.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/input">
<output>
<xsl:apply-templates select="*[@value='true']"/>
</output>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And here's input.xml
:
<?xml version="1.0" encoding="utf-8" ?>
<input>
<element value="true">
<p>This will get copied to the output.</p>
<p>Note that the use of the identity transform means that all of this content
gets copied to the output simply because templates were applied to the
<em>element</em> element.
</p>
</element>
<element value="false">
<p>This, on the other hand, won't get copied to the output.</p>
</element>
</input>