C# 4.0 SAXON XSLT错误

C# 4.0 SAXON XSLT错误,c#-4.0,saxon,C# 4.0,Saxon,我有以下测试代码,试图将cvs文件转换为xml。我遇到的问题是,有时它会运行,有时它会失败,并出现以下错误: The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type. 代码如下: XsltCompiler compiler = null; try {

我有以下测试代码,试图将cvs文件转换为xml。我遇到的问题是,有时它会运行,有时它会失败,并出现以下错误:

The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.
代码如下:

        XsltCompiler compiler = null;
        try
        {
            Processor processor = new Processor();
            compiler = processor.NewXsltCompiler();

            var sr = new StreamReader(@"c:\files\drop\csv-to-xml_v2.xslt");
            var xslt = sr.ReadToEnd();
            sr.Close();

            StringReader reader = new StringReader(xslt);

            XsltExecutable exec = compiler.Compile(reader);

            XsltTransformer transformer = exec.Load();

            transformer.InitialTemplate = new QName("", "main");

            var XmlResult = new DomDestination();

            transformer.Run(XmlResult);

            reader.Close();
        }
        catch (Exception ex)
        {
            var errMsg = ex.Message;
            var errList = compiler != null ? compiler.ErrorList : null;
        }
XSLT是:

<?xml version="1.0"?>

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="fn"
exclude-result-prefixes="xs fn">

<xsl:output indent="yes" encoding="US-ASCII"/>

<xsl:param name="pathToCSV" select="'file:///C:/Files/Drop/inputcsv1.csv'"/>

<xsl:function name="fn:getTokens" as="xs:string+">
<xsl:param name="str" as="xs:string"/>
    <xsl:analyze-string select="concat($str, ',')" regex='(("[^"]*")+|[^,]*),'>
        <xsl:matching-substring>
            <xsl:sequence select='replace(regex-group(1), "^""|""$|("")""", "$1")'/>
        </xsl:matching-substring>
    </xsl:analyze-string>
</xsl:function>

<xsl:template match="/" name="main">
<xsl:choose>
    <xsl:when test="unparsed-text-available($pathToCSV)">
        <xsl:variable name="csv" select="unparsed-text($pathToCSV)"/>
        <xsl:variable name="lines" select="tokenize($csv, '&#xD;')" as="xs:string+"/>
        <xsl:variable name="elemNames" select="fn:getTokens($lines[1])" as="xs:string+"/>
        <root>
            <xsl:for-each select="$lines[position() > 0]">
                <row>
                    <xsl:variable name="lineItems" select="fn:getTokens(.)" as="xs:string+"/>

                    <xsl:for-each select="$elemNames">
                        <xsl:variable name="pos" select="position()"/>
                        <column>
                            <xsl:value-of select="$lineItems[$pos]"/>
                        </column>
                    </xsl:for-each>
                </row>
            </xsl:for-each>
        </root>
    </xsl:when>
    <xsl:otherwise>
        <xsl:text>Cannot locate : </xsl:text><xsl:value-of select="$pathToCSV"/>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

找不到:
有人知道为什么有时候它能工作,但在“transformer.Run(XmlResult)”行中却无缘无故地失败了吗?我正在使用c#4、Visual Studio 2010和Saxon 9 HE。

在“否则”分支中,您正在创建一个文本节点作为文档节点的子节点。因为您的目的地是DOM目的地,所以这是不允许的。(它在XDM数据模型中是允许的,但在DOM中无法表示)