我有一个要写入文件的XDocument(在下面的“ // XDocument”注释下列出),但是一旦我使用了下面在“ // writing method”注释下写的写入方法,我得到了不完整的结尾(例如“ </>
”而不是完整的结尾(例如“ </script>"
)。我需要以某种方式解决此问题。
我的代码:
private static void Execute()
{
string outputXMLFile = outputXMLFile = @"C:\Temp\testPoints6.html";
//XDocument
XDocument documentElement =
new XDocument(
new XDeclaration(
"1.0",
Encoding.Default.ToString(), null
)
,
new XDocumentType(
"html", null, null, null
));
XElement html = new XElement("html");
XElement head = new XElement("head");
html.Add(new XAttribute("lang", "ru"));
head.Add(
new XElement("title",
"Exported X3DOM Scene"));
head.Add(
new XElement("meta",
new XAttribute("http-equiv", "X-UA-Compatible"),
new XAttribute("content", "chrome=1")));
head.Add(
new XElement("meta",
new XAttribute("http-equiv", "Content-Type"),
new XAttribute("content", "text/html;charset=utf-8")));
head.Add(
new XElement("link",
new XAttribute("rel", "stylesheet"),
new XAttribute("type", "text/css"),
new XAttribute("href", "http://www.x3dom.org/x3dom/release/x3dom.css")));
head.Add(
new XElement("script",
new XAttribute("type", "text/javascript"),
new XAttribute("src", "http://www.x3dom.org/x3dom/release/x3dom.js")
));
html.Add(head);
documentElement.Add(html);
//writing method
XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = Encoding.Default;
xws.Indent = true;
xws.NewLineOnAttributes = true;
using ( XmlWriter xw = XmlWriter.Create(outputXMLFile, xws) ) {
documentElement.WriteTo(xw);
xw.Flush();
}
}
输出:
<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE html >
<html
lang="ru">
<head>
<title>Exported X3DOM Scene</title>
<meta
http-equiv="X-UA-Compatible"
content="chrome=1" />
<meta
http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<link
rel="stylesheet"
type="text/css"
href="http://www.x3dom.org/x3dom/release/x3dom.css" />
<script
type="text/javascript"
src="http://www.x3dom.org/x3dom/release/x3dom.js" />
</head>
</html>
因此,我需要这个XmlWriter来将“ <script> </script>
”写入文件而不是“ <script .../>
”,这是不正确的,并且在我在浏览器中运行它时会造成一些麻烦
我采取了“解决方案”,在这篇博文中添加了结束标记:“ How do you force explicit tag closing with Linq XML?”,但似乎不起作用
答案 0 :(得分:0)
好,我解决了。
我编写了2个函数来为整个文档添加完整的结尾:
args