1

Is there any possible way to write an attribute without value:

<element specified/>

I believe I can do it with:

writer.WriteRaw("<element specified/>")

But is there is any way to do it with WriteAttributeString, WriteStartAttribute, WriteAttributes or other methods?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Vitaliy
  • 2,744
  • 1
  • 24
  • 39
  • possible duplicate of [is an xml attribute without value valid?](http://stackoverflow.com/questions/6926442/is-an-xml-attribute-without-value-valid) – Damith Nov 14 '11 at 12:25
  • 6
    `` is not well-formed XML and cannot be parsed by any XML software, they'll all raise an error. It is, however, valid SGML (and sometimes valid HTML). Using WriteRaw is the only way to bend the rules and write illegal XML. – Abel Nov 14 '11 at 12:26
  • 1
    @UnhandledException - the "duplicate" question is unrelated to the question I wrote - how to create boolean attribute with XmlWriter – Vitaliy Nov 14 '11 at 12:33

1 Answers1

1

If you need to write old-style SGML structure, you may consider using HtmlTextWriter, or any of its descendants, like XhtmlTextWriter. Their original intentions were for use in ASP.NET, but since they derive from TextWriter, they should be usable in other scenario's as well.

If you need even more flexibility, it would be nice to have an SGMLWriter. Unfortunately, I was only able to find an SGMLReader.

Edit

You can create an XmlWriter that writes valid HTML which isn't valid XML, as in HTML 3.2 or 4.0. You can do so by overwriting OutputMethod using reflection (it is a read-only property, it won't let you do it otherwise, as far as I found). Example:

XmlWriterSettings settings = new XmlWriterSettings();

// Use reflection, not that you need GetMethod, not GetProperty
MethodInfo propOutputMethod = settings.GetType().GetMethod("set_OutputMethod", 
    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod);
propOutputMethod.Invoke(settings, new object[] { XmlOutputMethod.Html });

// check value, it should contain XmlOutputMethod.Html now
var outputMethod = settings.OutputMethod;

// continue as you used to do
StringBuilder builder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(builder, settings);
writer.WriteStartDocument();
writer.WriteStartElement("html");
writer.WriteStartElement("input");
writer.WriteAttributeString("ismap", "value");
writer.WriteEndElement();
writer.WriteElementString("br", null);
writer.WriteElementString("img", null);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();

// variable output will now contain "<html><input ismap><br><img></html>"
string output = builder.ToString();

Important note: this will only work for predefined attributes and tags. As far as I know, this can only be done with the ones defined in the HTML 4.0 specification as boolean or as tag not requiring closing (br, img, hr etc).

In addition, if you try to create illegal combinations, it will use standard syntax. It is not possible to force a boolean attribute without ="". I.e., this works: <input ismap>, this doesn't <inputs ismap=""></inputs>.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • probably you're right and I have to use something like HtmlTextWriter. I was wondering that XmlWriterSettings.OutputMethod readonly property accepts Xml, Text, Html. So probably XmlWriter allows also write non-well formed HTML? – Vitaliy Nov 14 '11 at 14:13
  • 1
    @Vitaliy: I tested setting the `OutputMethod` to a different value. It writes HTML, but so far it's also valid XHTML and I couldn't get it to write empty optional attributes. However, if you look up the documentation, you'll see this setting is used only internally by `XsltProcessor` of .NET. Internally, it uses the `HtmlUtf8RawTextWriter` (hidden class). – Abel Nov 14 '11 at 15:42