如何用XmlWriter编写布尔属性(指定-未指定)?
有没有可能的方法来编写没有值的属性:
<element specified/>
我相信我可以这样做:
writer.WriteRaw("<element specified/>")
但是有没有办法用 WriteAttributeString、WriteStartAttribute、WriteAttributes 或其他方法来做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要编写旧式 SGML 结构,您可以考虑使用 HtmlTextWriter 或其任何后代,例如 XhtmlTextWriter。它们的初衷是在 ASP.NET 中使用,但由于它们派生自 TextWriter,因此它们也应该可以在其他场景中使用。
如果您需要更大的灵活性,那么最好有一个 SGMLWriter。不幸的是,我只能找到一个 SGMLReader。
编辑
您可以创建一个 XmlWriter 来编写有效的 HTML,但不是有效的 XML,如 HTML 3.2 或 4.0 中。您可以通过使用反射覆盖
OutputMethod
来实现此目的(它是一个只读属性,据我发现,否则它不会让您这样做)。示例:重要提示:这仅适用于预定义的属性和标签。据我所知,这只能通过 HTML 4.0 规范中定义为布尔值或不需要关闭的标签(br、img、hr 等)来完成。
此外,如果您尝试创建非法组合,它将使用标准语法。如果没有
=""
,则无法强制使用布尔属性。即,这有效:,这不起作用
。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: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>
.