【问题标题】:Xslt cant see assembly referenceXslt 看不到程序集参考
【发布时间】:2012-07-07 23:23:58
【问题描述】:

在我的 xslt 中,我有一段 C# 代码:

public string CleanForTableTags(string pContent){

        string input = String.Concat("<root>", pContent, "</root>");

    System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(input, System.Xml.Linq.LoadOptions.PreserveWhitespace);

    var valueWithinTags = doc.Root.Element("table").ToString();
    string[] values = Regex.Matches(valueWithinTags, @"<.*?>")
        .Cast<Match>()
        .Select(o => o.Groups[0].Value)
        .ToArray();

    System.Guid d = System.Guid.NewGuid();
    string s = pContent.Replace(valueWithinTags, d.ToString());

    return s;
}

你可以看到我使用:System.Xml.Linq

在我的 webconfig 中我得到了:

    <compilation debug="true">
        <assemblies>
            <add assembly="WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Drawing.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>
    </compilation>

我仍然得到:

命名空间“System.Xml”中不存在类型或命名空间名称“Linq”(您是否缺少程序集引用?)

我做错了什么?

我使用的是 VS 2008。所有到 Dll 的路径都是正确的,如果我使用类中的相同代码段,它可以正常工作,但是一旦在 xslt 内部使用,我就会出错

下一个问题:

现在失败了

string[] values = Regex.Matches(valueWithinTags, @"<.*?>")
            .Cast<Match>()
            .Select(o => o.Groups[0].Value)
            .ToArray();



'System.Text.RegularExpressions.MatchCollection' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Text.RegularExpressions.MatchCollection' could be found (are you missing a using directive or an assembly reference?)

【问题讨论】:

    标签: c# xslt reference .net-assembly


    【解决方案1】:

    “xslt 内部”到底是什么意思?

    XSLT 只是一个带有转换的 xml 文件。或者您是否使用 msxsl 命名空间来包含一段使用 C# 的脚本?

    在这种情况下,您可能需要包含&lt;msxsl:assembly name="System.Xml.Linq" /&gt; 以及在转换文件中使用&lt;msxsl:using namespace="System.Xml.Linq" /&gt;

    这是一个如何在转换中包含一段脚本的示例:http://docs.composite.net/FAQ/Developer?q=How+to+run+CSharp+function+in+XSLT%3F

    关于第二部分,关于Cast&lt;&gt;,这是System.Linq.Enumerable 中的扩展方法,因此将System.Linq 命名空间添加为“使用”就足够了(还要确保System.Core 也作为程序集包含在内)。

    【讨论】:

    • 你是对的,我只是忘了添加 using namespace :D 如果你能再回答 1 个问题,请。我会在几秒钟内更新问题
    • 刚刚添加了部分来解决您的第二个问题;-)。让我知道这是否仍然会导致问题。
    • 非常感谢,那是坚果饼干,用了将近 4 个小时! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多