Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove whitespace in self closing tags when writing xml document

When writing out an xml document I need to write all self closing tags without any whitespace, for example:

<foo/> 

instead of:

<foo />

The reason for this is that a vendor system that I'm interfacing with throws a fit otherwise. In an ideal world the vendor would fix their system, but I don't bet on that happening any time soon. What's the best way to get an XmlWriter to output the self closing tags without the space?

My current scheme is to do something like:

return xml.Replace(" />", "/>");

Obviously this is far from ideal. Is it possible to subclass the XmlWriter for that one operation? Is there a setting as part of the XmlWriterSettings that I've overlooked?

like image 583
jonnii Avatar asked Jul 06 '11 18:07

jonnii


People also ask

Does XML allow self closing tags?

In XML and XHTML, a self-closing tag is a shorthand notation for an opening and closing tag in one. It's used to communicate lack of content in between the opening and closing tags. So, rather than typing <p></p> (with no space at all in between), you'd be able write <p/> .

Is whitespace preserved in XML?

In XML documents, there are two types of whitespace: Significant whitespace is part of the document content and should be preserved. Insignificant whitespace is used when editing XML documents for readability. These whitespaces are typically not intended for inclusion in the delivery of the document.

Does XML need a closing tag?

All XML Elements Must Have a Closing Tag.

Can you have spaces in XML tags?

XML Naming Rules Element names cannot start with the letters xml (or XML, or Xml, etc) Element names can contain letters, digits, hyphens, underscores, and periods. Element names cannot contain spaces.


1 Answers

I think that there is no such option to avoid that one space in self-closing tag. According to MSDN, XmlTextWriter:

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.

Hopefully you could write <elementName></elementName> syntax instead of unwanted <elementName />, to do that use XmlWriter.WriteFullEndElement method, e.g.:

using System.Xml;
..

static void Main(string[] args)
{
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
    xmlWriterSettings.Indent = true;
    xmlWriterSettings.IndentChars = ("\t");
    xmlWriterSettings.OmitXmlDeclaration = true;
    XmlWriter writer = XmlWriter.Create("example.xml", xmlWriterSettings);

    writer.WriteStartElement("root");

    writer.WriteStartElement("element1");
    writer.WriteEndElement();

    writer.WriteStartElement("element2");
    writer.WriteFullEndElement();

    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Close();
}

produces following XML document:

<root>
    <element1 />
    <element2></element2>
</root>
like image 102
Grzegorz Szpetkowski Avatar answered Sep 23 '22 17:09

Grzegorz Szpetkowski