You can just use an if statement:
if (x == 42)
Foo("yo", a, b, c);
else
Foo("yo", a, b);
You can't use the ?: operator in this case (at least, outside of the function call), because Foo has no return value. The ?: operator must evaluate to something, and that something must be assigned to something else.
Another option that would get rid of the duplicate function call is to use an array or list for the params:
var parameters = new List<object> { a, b };
if (x == 42)
parameters.Add(c);
Foo("yo", parameters);
And if you really wanted to get ?: in there, this would work, too:
Foo("yo", x == 42 ? new object[] { a, b, c } : new object[] { a, b });
For your more specific question about the XDocument/XElement constructor calls, you might want to use Add calls rather than one long series of constructor calls. Then you can make them conditional. As is, I think you should also be able to do what you're asking by doing something like this:
XElement elementPrecedingOptionalElement = new XElement(...);
var xml = new XDocument(....,
new XElement(...),
new XElement(...),
elementPrecedingOptionalElement,
new XElement(...),
new XElement(...)
);
if (x == 42)
elementPrecedingOptionalElement.AddAfterSelf(new XElement(...));
Using Add calls would look something like this:
XDocument xml = new XDocument();
XElement root = new XElement("Root");
xml.Add(root);
root.Add(new XElement("Item1"));
root.Add(new XElement("Item2"));
if (x == 42)
root.Add(new XElement("Item2.5"));
root.Add(new XElement("Item3"));
root.Add(new XElement("Item4"));
Actually, one last version that is much closer to what you are asking for would be like this, which seems to work:
var xml = new XDocument(....,
new XElement(...),
new XElement(...),
new XElement(...),
x == 42
? new XElement(...)
: null,
new XElement(...),
new XElement(...)
);