[英]Predefined type 'System.Object' is not defined or imported .net 4.6
[英].NET Xsl script transform results in 'System.Object' is not defined or imported
XSL noobie但是卡住了!
我有一個使用c#腳本函數格式化日期的轉換,當我在VS 2008並運行“show xml output”時這很好用,輸出正是我想要的。
但是,當我嘗試使用代碼運行時,我得到錯誤
未定義或導入預定義類型“System.Object”
要調用轉換的函數看起來像這樣,它在我開始使用腳本之前非常基礎和工作
public static string RunXSLT(string xsltFile, string inputXML) { XslCompiledTransform transform = new XslCompiledTransform(); XsltSettings settings = new XsltSettings(); settings.EnableScript = true; transform.Load(xsltFile, settings, null); StringReader sReader = new StringReader(inputXML); XmlTextReader xmlTextReader = new XmlTextReader(sReader); //Create an XmlTextWriter which outputs to memory stream Stream stream = new MemoryStream(); XmlWriter xmlWriter = new XmlTextWriter(stream,> System.Text.Encoding.UTF8); transform.Transform(xmlTextReader, xmlWriter); stream.Position = 0; XmlDocument XmlDoc = new XmlDocument(); XmlDoc.Load(stream); return XmlDoc.OuterXml; }
XSL轉換就是這個..
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:nlbExtension="urn:nlbExtension" exclude-result-prefixes="msxsl nlbExtension"> <xsl:output method="xml" indent="yes"/> <msxsl:script implements-prefix="nlbExtension" language="C#"> <![CDATA[ public string FormatDateTime(string xsdDateTime, string format) { DateTime date = DateTime.Parse(xsdDateTime); return date.ToString(format); } ]]> </msxsl:script> <xsl:template match="/"> <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" <xsl:for-each select="./Collection/Content" > <url> <loc>http://www.nlb.org<xsl:value-of select="./QuickLink/text()"/></loc> <lastmod><xsl:value-of select="./DateModified/text()" /></lastmod> </url> </xsl:for-each> </urlset> </xsl:template> </xsl:stylesheet>
我知道這個問題已經很老了,但也許這會幫助那些正在尋找這個問題的人。
我目前變成了相同的編譯錯誤,但完全不同的C#編程主題。
我正在使用Sharp Develop 4.2並遇到同樣的問題。 對我來說,解決方案是將“mscorlib”添加到項目的引用中。
此錯誤是Microsoft的已知問題,但不知道當前的情況。 剛剛在2010年發現了一些討論。
不知怎的,這個引用遺失了,我現在還不完全理解為什么我的其他項目沒有明確引用mscorlib,但也許這確實是bug本身;-)
最好的問候托馬斯
無法重現(實際上,您實際上並沒有在xslt中使用擴展名)。 我測試了它,添加(到xslt):
<xsl:value-of select="nlbExtension:FormatDateTime(.,'dd MMM yyyy')"/>
並使用輸入xml:
string xml = new XElement("xml", DateTime.Now).ToString();
它工作正常(我改為XmlConvert.ToDateTime
以匹配xsd格式,但無論哪種方式都可以正常工作)。
如果出現問題,則表示您沒有向我們展示。
這是一個更簡單的例子,(抱歉打字錯誤),再次這在VS2008中適用於對樣式表運行XML但是使用我得到的C#代碼
未定義或導入預定義類型“System.Object”
碼
public void RunFileXSLT() { // Open books.xml as an XPathDocument. XPathDocument doc = new XPathDocument("c:\\\\temp\\\\raw.xml"); // Create a writer for writing the transformed file. XmlWriter writer = XmlWriter.Create("c:\\\\temp\\\\OutputTest.xml"); // Create and load the transform with script execution enabled. XslCompiledTransform transform = new XslCompiledTransform(); XsltSettings settings = new XsltSettings(); settings.EnableScript = true; transform.Load("c:\\\\temp\\\\Simple.xslt", settings, null); // Execute the transformation. transform.Transform(doc, writer); }
xml就是這個
<xml> <item> <date>11/11/2009</date> </item> <item> <date>11/11/2009</date> </item> </xml>
變換是這樣的
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:nlbExtension="urn:nlbExtension" exclude-result-prefixes="msxsl nlbExtension"> <xsl:output method="xml" indent="yes"/> <msxsl:script implements-prefix="nlbExtension" language="C#"> <![CDATA[ public string FormatDateTime(string xsdDateTime, string format) { DateTime date = DateTime.Parse(xsdDateTime); return date.ToString(format); } ]]> </msxsl:script> <xsl:template match="/"> <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" > <xsl:for-each select="./xml/item" > <url> <loc>http://www.a-website.com</loc> <lastmod> <xsl:value-of select="nlbExtension:FormatDateTime(./date,'s')"/> </lastmod> </url> </xsl:for-each> </urlset>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.