如何用XmlWriter编写布尔属性(指定-未指定)?

发布于 2024-12-15 20:38:29 字数 298 浏览 0 评论 0原文

有没有可能的方法来编写没有值的属性:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-12-22 20:38:29

如果您需要编写旧式 SGML 结构,您可以考虑使用 HtmlTextWriter 或其任何后代,例如 XhtmlTextWriter。它们的初衷是在 ASP.NET 中使用,但由于它们派生自 TextWriter,因此它们也应该可以在其他场景中使用。

如果您需要更大的灵活性,那么最好有一个 SGMLWriter。不幸的是,我只能找到一个 SGMLReader

编辑

您可以创建一个 XmlWriter 来编写有效的 HTML,但不是有效的 XML,如 HTML 3.2 或 4.0 中。您可以通过使用反射覆盖 OutputMethod 来实现此目的(它是一个只读属性,据我发现,否则它不会让您这样做)。示例:

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();

重要提示:这仅适用于预定义的属性和标签。据我所知,这只能通过 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:

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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文