When creating a doctype for an System.Xml.Linq.XDocument like this:
为System.Xml.Linq创建doctype时。XDocument是这样的:
doc.AddFirst(new XDocumentType("html", null, null, null));
The resulting saved XML file starts with:
结果保存的XML文件以以下内容开始:
<!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 :)
注意在结束角括号之前的额外空间。我怎样才能避免这个空间的出现?如果可能的话,我想要一种干净的方式:
3 个解决方案
#1
2
One approach is to write a wrapper class for the XmlWriter. So:
一种方法是为XmlWriter编写一个包装类。所以:
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:
然后,对于MyXmlWriterWrapper类,在XmlWriter类接口上定义每个方法,以便将调用直接传递给包装的writer,除了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);
}
}
Not an especially clean solution admittedly, but it'll do the job.
无可否认,这并不是一个特别干净的解决方案,但它将发挥作用。
#2
4
You don't get the space if you write to an XmlTextWriter:
如果您写入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();
}
#3
0
I might be wrong but I think this space is because there are more parameters expected after the HTML. Although HTML5 allows it.
我可能错了,但是我认为这个空间是因为HTML之后有更多的参数。虽然HTML5允许它。
Try specifying at least the 3rd parameter as well (*.dtd). Or do something like this:
尝试至少指定第3个参数(*.dtd)。或者做这样的事情:
new XDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null)
#1
2
One approach is to write a wrapper class for the XmlWriter. So:
一种方法是为XmlWriter编写一个包装类。所以:
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:
然后,对于MyXmlWriterWrapper类,在XmlWriter类接口上定义每个方法,以便将调用直接传递给包装的writer,除了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);
}
}
Not an especially clean solution admittedly, but it'll do the job.
无可否认,这并不是一个特别干净的解决方案,但它将发挥作用。
#2
4
You don't get the space if you write to an XmlTextWriter:
如果您写入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();
}
#3
0
I might be wrong but I think this space is because there are more parameters expected after the HTML. Although HTML5 allows it.
我可能错了,但是我认为这个空间是因为HTML之后有更多的参数。虽然HTML5允许它。
Try specifying at least the 3rd parameter as well (*.dtd). Or do something like this:
尝试至少指定第3个参数(*.dtd)。或者做这样的事情:
new XDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null)