xDocument (.NET)에 HTML 5 DocType 추가
-
07-07-2019 - |
문제
system.xml.linq.xdocument에 대한 doctype를 만들 때 : 다음과 같이합니다.
doc.AddFirst(new XDocumentType("html", null, null, null));
결과 저장된 XML 파일은 다음과 같이 시작합니다.
<!DOCTYPE html >
닫는 각도 브래킷 전에 추가 공간을 주목하십시오. 이 공간이 나타나는 것을 어떻게 방지 할 수 있습니까? 가능하면 깨끗한 방법을 원합니다 :)
해결책
한 가지 방법은 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);
}
}
특히 깨끗한 솔루션은 아니지만 작업을 수행 할 것입니다.
다른 팁
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();
}
틀릴 수도 있지만이 공간은 HTML 이후에 더 많은 매개 변수가 있기 때문이라고 생각합니다. HTML5가 허용하지만.
최소한 세 번째 매개 변수 (*.dtd)를 지정하십시오. 또는 다음과 같은 일을하십시오.
new XDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null)
제휴하지 않습니다 StackOverflow