将XML转换为字符串,并将XSLT转换为C#.NET 4.0中的字符串-路径中的字符无效
我将XML文件定义为:将XML转换为字符串,并将XSLT转换为C#.NET 4.0中的字符串-路径中的字符无效,xml,asp.net-mvc,c#-4.0,xslt,Xml,Asp.net Mvc,C# 4.0,Xslt,我将XML文件定义为: <?xml version="1.0" encoding='UTF-8'?> <contentlets> <content> <entry> <string>link</string> <string>http://www.myLink.com</string> </entry> <entry> <string&
<?xml version="1.0" encoding='UTF-8'?>
<contentlets>
<content>
<entry>
<string>link</string>
<string>http://www.myLink.com</string>
</entry>
<entry>
<string>stInode</string>
<string>0b4f59c1-6dee-4c1e-a0fb-353c34c8d372</string>
</entry>
<entry>
<string>linkType</string>
<string>Category Title</string>
</entry>
<entry>
<string>linkText</string>
<string>Title</string>
</entry>
</content>
</contentlets>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@* | node()">
<div id="content" data-role="content">
<ul data-role="listview" data-inset="true" data-filter="false">
<li data-role="list-divider">Category Title</li>
<xsl:for-each select="content">
<li data-inline="false" data-icon="arrow-r" data-iconpos="right">
<a target="_blank">
<xsl:for-each select="entry">
<xsl:if test="string='link'">
<xsl:attribute name="href">
<xsl:value-of select="string[2]"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="string='linkText'">
<h3>
<xsl:value-of select="string[2]"/>
</h3>
</xsl:if>
</xsl:for-each>
</a>
</li>
</xsl:for-each>
</ul>
</div>
</xsl:template>
</xsl:stylesheet>
然后,我调用以下命令来实际应用转换:
string strXslt = String.Empty;
string strXml = String.Empty;
using (StreamReader xsltReader = new StreamReader(Server.MapPath("~/Content/xsl/test.xslt")))
{
strXslt = xsltReader.ReadToEnd();
}
using (StreamReader xmlReader = new StreamReader(Server.MapPath("~/Content/xml/test.xml")))
{
strXml = xmlReader.ReadToEnd();
}
XsltTransformer transformer = new XsltTransformer(strXslt);
return transformer.Transform(strXml);
当应用转换时,我得到一个“路径中的非法字符”错误。我知道我的XML和XSLT文件是以字符串的形式正确传递的,我只是不明白为什么在应用转换时会出现这个错误
你知道是什么导致了这个错误吗
编辑:
以下是堆栈跟踪:
[ArgumentException: Illegal characters in path.]
System.IO.Path.CheckInvalidPathChars(String path) +126
System.IO.Path.Combine(String path1, String path2) +38
System.Web.Compilation.DiskBuildResultCache.GetPreservedDataFileName(String cacheKey) +27
System.Web.Compilation.DiskBuildResultCache.GetBuildResult(String cacheKey, VirtualPath virtualPath, Int64 hashCode, Boolean ensureIsUpToDate) +14
System.Web.Compilation.BuildManager.GetBuildResultFromCacheInternal(String cacheKey, Boolean keyFromVPP, VirtualPath virtualPath, Int64 hashCode, Boolean ensureIsUpToDate) +200
System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate) +51
System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +68
System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +111
System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +125
System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound) +35
System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath) +9
System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath) +41
System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List`1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations) +150
System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) +304
System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) +136
System.Web.Mvc.<>c__DisplayClassc.<FindView>b__b(IViewEngine e) +24
System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths) +127
System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName) +181
System.Web.Mvc.ViewResult.FindView(ControllerContext context) +138
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +129
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8836913
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
[ArgumentException:路径中的非法字符。]
System.IO.Path.CheckInvalidPathChars(字符串路径)+126
System.IO.Path.Combine(字符串路径1、字符串路径2)+38
System.Web.Compilation.DiskBuildResultCache.GetPreservedDataFileName(字符串缓存键)+27
System.Web.Compilation.DiskBuildResultCache.GetBuildResult(字符串缓存键、VirtualPath、VirtualPath、Int64哈希代码、布尔值和最新值)+14
System.Web.Compilation.BuildManager.GetBuildResultFromCacheInternal(String cacheKey,Boolean keyFromVPP,VirtualPath VirtualPath,Int64 hashCode,Boolean Update)+200
System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath VirtualPath,布尔值)51
System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath、Boolean noBuild、Boolean allowCrossApp、Boolean allowBuildInPrecompile、Boolean throwIfNotFound、Boolean EnsureUpdate)+68
System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext上下文、VirtualPath VirtualPath、Boolean noBuild、Boolean allowCrossApp、Boolean allowBuildInPrecompile、Boolean throwIfNotFound、Boolean EnsureUpdate)+111
System.Web.Compilation.BuildManager.GetVirtualPath对象工厂(VirtualPath VirtualPath,HttpContext上下文,布尔allowCrossApp,布尔throwIfNotFound)+125
System.Web.Compilation.BuildManager.GetObjectFactory(字符串virtualPath,布尔throwIfNotFound)+35
System.Web.Mvc.buildmanagerRapper.System.Web.Mvc.IBuildManager.FileExists(字符串virtualPath)+9
System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext ControllerContext,字符串virtualPath)+41
System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext ControllerContext,列表'1位置,字符串名称,字符串控制器名称,字符串区域名称,字符串缓存键,字符串[]和搜索位置)+150
System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext ControllerContext,String[]位置,String[]区域位置,String位置PropertyName,String名称,String controllerName,String cacheKeyPrefix,Boolean useCache,String[]和searchedLocations)+304
System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext ControllerContext,String viewName,String masterName,Boolean useCache)+136
System.Web.Mvc.c____________________________________________________________
System.Web.Mvc.ViewEngineCollection.Find(Func`2查找,布尔跟踪搜索路径)+127
System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext ControllerContext,String viewName,String masterName)+181
System.Web.Mvc.ViewResult.FindView(ControllerContext上下文)+138
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文)+129
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext ControllerContext,ActionResult ActionResult)+13
System.Web.Mvc.c__显示Class1C.b__19()+23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器,ResultExecutingContext预文本,Func`1 continuation)+260
System.Web.Mvc.c__DisplayClass1e.b__1b()+19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext ControllerContext,IList`1过滤器,ActionResult ActionResult)+177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+343
System.Web.Mvc.Controller.ExecuteCore()+116
System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext RequestContext)+10
System.Web.Mvc.c__显示类b.b__5()+37
System.Web.Mvc.Async.c__显示类1.b__0()+21
System.Web.Mvc.Async.c_uuudisplayClass8`1.b_uuu7(IAsyncResult)+12
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62
System.Web.Mvc.c_uuudisplayClasse.b_uuud()+50
System.Web.Mvc.SecurityUtil.b___0(操作f)+7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(操作)+22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+60
System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest(IAsyncResult结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+8836913
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+184
更换:
<a target="_blank">
<xsl:for-each select="entry">
<xsl:if test="string='link'">
<xsl:attribute name="href">
<xsl:value-of select="string[2]"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="string='linkText'">
<h3>
<xsl:value-of select="string[2]"/>
</h3>
</xsl:if>
</xsl:for-each>
</a>
到
替换:
<a target="_blank">
<xsl:for-each select="entry">
<xsl:if test="string='link'">
<xsl:attribute name="href">
<xsl:value-of select="string[2]"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="string='linkText'">
<h3>
<xsl:value-of select="string[2]"/>
</h3>
</xsl:if>
</xsl:for-each>
</a>
到
考虑在异常发生时发布堆栈跟踪。无论是<代码> xScCulieldDebug 方法,也不是您的<代码> XsltTransformer <代码>方法,如“代码>转换/代码>出现在堆栈跟踪中,因此它看起来不象是与您所发布的XSLT特定代码相关的。我不能重复(在ASP.NET MVC 4中)-我假设
返回transformer.Transform(strXml)代码>从具有字符串返回类型的控制器方法返回。这将返回转换后的HTML,IE将其完美呈现。可能需要检查.xslt和.xml文件上的实际编码是否与规定的编码(utf-8)匹配,如果文件名中有任何可转义字符,如果它们不是test.xml/test.xslt,则在文件路径前面加上“@”。这是一个ArgumentException,因此对于某些对象的ctor,您传递的不是文件路径,而是xml字符串@Mark:XslCompiledTransform的转换方法是使用XmlReader和XmlWriter类型的参数,而不是任何字符串。据我所知,这是正确使用变换方法。
<xsl:element name="a">
<xsl:attribute name="target">
<xsl:text>_blank</xsl:text>
</xsl:attribute>
<xsl:if test="./entry/string[1]/text()[.='link']">
<xsl:attribute name="href">
<xsl:value-of select="(./entry[./string[1]/text()='link']/string[2]/text())[1]"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="./entry/string[1]/text()[.='linkText']">
<xsl:element name="h3">
<xsl:value-of select="(./entry[./string[1]/text()='linkText']/string[2]/text())[1]"/>
</xsl:element>
</xsl:if>
</xsl:element>
using (StringWriter sw = new StringWriter())
using (XmlWriter xwo = XmlWriter.Create(sw, xslTransform.OutputSettings))
{
xslTransform.Transform(xri, xwo);
output = sw.ToString();
}
using (StringWriter sw = new StringWriter())
{
using (XmlWriter xwo = XmlWriter.Create(sw, xslTransform.OutputSettings))
{
xslTransform.Transform(xri, xwo);
}
output = sw.ToString();
}