HTML 5 DOCTYPE加入的XDocument(.NET) [英] Add HTML 5 doctype to XDocument (.NET)

查看:99
本文介绍了HTML 5 DOCTYPE加入的XDocument(.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当像这样的System.Xml.Linq.XDocument创建的文档类型:

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

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

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

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 :)

推荐答案

一种方法是写的一个包装类的XmlWriter。所以:

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

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

然后用于MyXmlWriterWrapper类定义的XmlWriter类接口上的每个方法直通到包裹作家传递呼叫,除了WriteDocType方法。然后,您可以定义为是这样的:

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.

这篇关于HTML 5 DOCTYPE加入的XDocument(.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆