将 HTML 5 文档类型添加到 XDocument (.NET)

发布于 2024-08-04 12:53:32 字数 264 浏览 4 评论 0原文

为 System.Xml.Linq.XDocument 创建文档类型时,如下所示:

doc.AddFirst(new XDocumentType("html", null, null, null));

生成的保存的 XML 文件开头为:

<!DOCTYPE html >

请注意右尖括号之前的额外空格。 如何防止这个空间出现? 如果可能的话,我想要一种干净的方式:)

When creating a doctype for an System.Xml.Linq.XDocument like this:

doc.AddFirst(new XDocumentType("html", null, null, null));

The resulting saved XML file starts with:

<!DOCTYPE html >

Notice the extra space before the closing angle bracket.
How can I prevent this space appearing?
I'd like a clean way if possible :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

时光磨忆 2024-08-11 12:53:32

如果写入 XmlTextWriter,则不会获得空间:

XDocument doc = new XDocument();
doc.AddFirst(new XDocumentType("html", null, null, null));
doc.Add(new XElement("foo", "bar"));

using (XmlTextWriter writer = new XmlTextWriter("c:\\temp\\no_space.xml", null)) {
    writer.Formatting = Formatting.Indented;
    doc.WriteTo(writer);
    writer.Flush();
    writer.Close();
}

You don't get the space if you write to an XmlTextWriter:

XDocument doc = new XDocument();
doc.AddFirst(new XDocumentType("html", null, null, null));
doc.Add(new XElement("foo", "bar"));

using (XmlTextWriter writer = new XmlTextWriter("c:\\temp\\no_space.xml", null)) {
    writer.Formatting = Formatting.Indented;
    doc.WriteTo(writer);
    writer.Flush();
    writer.Close();
}
因为看清所以看轻 2024-08-11 12:53:32

一种方法是为 XmlWriter 编写一个包装类。因此:

XmlWriter writer = new MyXmlWriterWrapper(XmlWriter.Create(..., settings))

然后,对于 MyXmlWriterWrapper 类,定义 XmlWriter 类接口上的每个方法,以将调用直接传递给包装的编写器(WriteDocType 方法除外)。然后,您可以将其定义为:

public override void WriteDocType(string name, string pubid, string sysid, string subset)
{
    if ((pubid == null) && (sysid == null) && (subset == null))
    {
        this.wrappedWriter.WriteRaw("<!DOCTYPE HTML>");
    }
    else
    {
        this.wrappedWriter.WriteDocType(name, pubid, sysid, subset);
    }
}

诚然,这不是一个特别干净的解决方案,但它可以完成工作。

One approach is to write a wrapper class for the XmlWriter. So:

XmlWriter writer = new MyXmlWriterWrapper(XmlWriter.Create(..., settings))

Then for the MyXmlWriterWrapper class define each method on the XmlWriter class interface to pass the call straight through to the wrapped writer, except for the WriteDocType method. You can then define that as something like:

public override void WriteDocType(string name, string pubid, string sysid, string subset)
{
    if ((pubid == null) && (sysid == null) && (subset == null))
    {
        this.wrappedWriter.WriteRaw("<!DOCTYPE HTML>");
    }
    else
    {
        this.wrappedWriter.WriteDocType(name, pubid, sysid, subset);
    }
}

Not an especially clean solution admittedly, but it'll do the job.

给不了的爱 2024-08-11 12:53:32

我可能是错的,但我认为这个空间是因为 HTML 之后需要更多参数。尽管 HTML5 允许。

尝试至少指定第三个参数 (*.dtd)。
或者做这样的事情:

new XDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null)

I might be wrong but I think this space is because there are more parameters expected after the HTML. Although HTML5 allows it.

Try specifying at least the 3rd parameter as well (*.dtd).
Or do something like this:

new XDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文