Fluent Interface for a XmlQueryBuilder In this answer I suggested using a fluent interface "builder" to replace all the hard-coded, repetitive and quite error-prone inline XML string concatenations.

This code might need a bit of tweaking to work perfectly with the OP's code in the question this code is following-up on, but it seems to work as expected. Thoughts?

public enum XmlQueryOperator
{
    Equals,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual
}




public class XmlQueryBuilder&lt;TEntity&gt;
    where TEntity : class
{
    private readonly XDocument _xDoc = new XDocument();
    private readonly XElement _queryNode = new XElement("query");

    public XmlQueryBuilder()
    {
        var rootNode = new XElement("queryxml");
        rootNode.SetAttributeValue("version", "1.0");

        var entityNode = new XElement("entity");
        entityNode.Value = typeof (TEntity).Name;

        rootNode.Add(entityNode);
        _xDoc.Add(rootNode);
    }

    public XmlQueryBuilder&lt;TEntity&gt; Where&lt;TProperty&gt;(Expression&lt;Func&lt;TEntity, TProperty&gt;&gt; property, XmlQueryOperator operation, string value)
    {
        var xCondition = new XElement("condition");
        var xField = new XElement("field");
        xField.Value = GetPropertyName(property);

        var xExpression = new XElement("expression");
        xExpression.SetAttributeValue("op", operation.ToString());
        xExpression.Value = value;

        xField.Add(xExpression);
        xCondition.Add(xField);

        _queryNode.Add(xCondition);
        return this;
    }

    public override string ToString()
    {
        var parent = _xDoc.Element("queryxml");
        if (parent == null) throw new InvalidOperationException("root node was not created.");

        parent.Add(_queryNode);
        return _xDoc.ToString();
    }

    private string GetPropertyName&lt;TSource, TProperty&gt;(Expression&lt;Func&lt;TSource, TProperty&gt;&gt; propertyLambda)
    {
        // credits http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression/672212#672212 (modified to return property name)
        var type = typeof(TSource);

        var member = propertyLambda.Body as MemberExpression;
        if (member == null)
            throw new ArgumentException(string.Format("Expression '{0}' refers to a method, not a property.", propertyLambda));

        var propInfo = member.Member as PropertyInfo;
        if (propInfo == null)
            throw new ArgumentException(string.Format("Expression '{0}' refers to a field, not a property.", propertyLambda));

        if (type != propInfo.ReflectedType &amp;&amp; !type.IsSubclassOf(propInfo.ReflectedType))
            throw new ArgumentException(string.Format("Expresion '{0}' refers to a property that is not from type {1}.", propertyLambda, type));

        return propInfo.Name;
    }
}




Usage - given a TestEntity:

public class TestEntity
{
    public string Test1 { get; set; }
    public int Test2 { get; set; }
    public DateTime Test3 { get; set; }
}


This program produces the expected output:

class Program
{
    static void Main(string[] args)
    {
        var builder = new XmlQueryBuilder&lt;TestEntity&gt;()
            .Where(t =&gt; t.Test1, XmlQueryOperator.Equals, "abc")
            .Where(t =&gt; t.Test2, XmlQueryOperator.GreaterThan, "def")
            .Where(t =&gt; t.Test3, XmlQueryOperator.LessThanOrEqual, DateTime.Now.ToString());

        Console.WriteLine(builder.ToString());
        Console.ReadLine();
    }
}


Output:

&lt;queryxml version="1.0"&gt;
  &lt;entity&gt;TestEntity&lt;/entity&gt;
  &lt;query&gt;
    &lt;condition&gt;
      &lt;field&gt;Test1&lt;expression op="Equals"&gt;abc&lt;/expression&gt;&lt;/field&gt;
    &lt;/condition&gt;
    &lt;condition&gt;
      &lt;field&gt;Test2&lt;expression op="GreaterThan"&gt;def&lt;/expression&gt;&lt;/field&gt;
    &lt;/condition&gt;
    &lt;condition&gt;
      &lt;field&gt;Test3&lt;expression op="LessThanOrEqual"&gt;2014-05-05 14:14:57&lt;/expression&gt;&lt;/field&gt;
    &lt;/condition&gt;
  &lt;/query&gt;
&lt;/queryxml&gt;
ToString() does too many things, it should only be calling _xDoc.ToString(); it has side-effects that make it dangerous to call more than once:

var foo = builder.ToString()
Console.WriteLine(builder.ToString());
Console.ReadLine();


Will not produce the expected output, and will not throw an exception either, resulting in an XML document with _queryNode showing up as many times as ToString() was called.