I have some XML that I am building that looks like this:
<actionitem actiontaken="none" target="0" targetvariable="0">
<windowname>Popup Window</windowname>
<windowposx>-1</windowposx>
<windowposy>-1</windowposy>
<windowwidth>-1</windowwidth>
<windowheight>-1</windowheight>
<noscrollbars>false</noscrollbars>
<nomenubar />
<notoolbar />
<noresize />
<nostatus />
<nolocation />
<browserWnd />
</actionitem>
This XML has to be to the exact specifications of the client, meaning I can't have a whitespace in the closing tag. I know that MSDN says this:
When writing an empty element, an additional space is added between tag name and the closing tag, for example . This provides compatibility with older browsers.
But, the client won't/can't budge on this. So, I thought I could try something like this to remedy the problem:
xelement.ReplaceWith(" />", "/>");
But when I run the program, I get this error message:
Non white space characters cannot be added to content.
So, does anyone know how I can remove that white space once I've built the XML document?
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.