0
我正在通過來自OpenXML Powertools的htmlconverter類創建一個XElement。在使用轉換器後修改XElement的問題
XElement html = HtmlConverter.ConvertToHtml(doc, settings)
現在,我嘗試添加一些新的節點像
html.Element("head").Add(new XElement("link",
new XAttribute("href", "stylesheet.css"),
new XAttribute("rel", "stylesheet"),
new XAttribute("type", "text/css")));
但html.Element(「頭」)總是返回空,我從Visual Studio中得到一個NullReferenceException錯誤在運行時調試模式,我仍然不知道爲什麼。
這是基於此article,我目前正在使用我的項目的代碼。我認爲它必須用htmlconverter做一些事情,因爲它可以工作,如果我創建一個手工製作的XElement。
// This example shows the simplest conversion. No images are converted.
// A cascading style sheet is not used.
byte[] byteArray = File.ReadAllBytes("Test.docx");
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc =
WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
XElement html = HtmlConverter.ConvertToHtml(doc, settings);
// Note: the XHTML returned by ConvertToHtmlTransform contains objects of type
// XEntity. PtOpenXmlUtil.cs defines the XEntity class. See
// http://blogs.msdn.com/ericwhite/archive/2010/01/21/writing-entity-references-using-linq-to-xml.aspx
// for detailed explanation.
//
// If you further transform the XML tree returned by ConvertToHtmlTransform, you
// must do it correctly, or entities do not serialize properly.
File.WriteAllText("Test.html", html.ToStringNewLineOnAttributes());
}
}
html.Firstnode的作品,所以我能夠創建自己的頭的XElement,並在HTML中firstnode替換它(這應該總是當頭)。目前它適用於我,但我仍然不滿意這個解決方案。 – user3120053