I don't know of a way to do it using an XElement
, the best option will be to read the Xml
as text but to avoid unnecessary excess string allocation, do it via a string builder:
var element = new XElement...;
var stringBuilder = new StringBuilder();
using (var stringWriter = new StringWriter(stringBuilder))
{
element.Save(stringWriter);
}
stringBuilder.Replace(" />", "/>");
var xml = stringBuilder.ToString();
Console.WriteLine(xml);
Any method which does a .ToString().Replace()
is going to be far more costly in terms of memory usage.
The worrying thing about your client's comment is that it sounds like they have a home made xml parser which isn't very good, the whitespace in a self closing tag should not make a difference.