C# 在C中使用XSLT合并具有不同节点结构的多个XML文件#

C# 在C中使用XSLT合并具有不同节点结构的多个XML文件#,c#,xml,xslt,transform,C#,Xml,Xslt,Transform,我在使用C#中的XSL transromation合并多个文件时遇到问题。我跟随了几个来自sevreal网站的例子和帖子,我很接近,但没有乐趣。我在运行代码时遇到一个错误,该代码声明“在添加属性节点之后,无法将命名空间节点添加到父元素。” 这是到目前为止我的代码 string[] filesInCurrentDir = Directory.GetFiles(dirsWithTestCases[a], "*.xml"); string[] xslFilePath = Directory.GetFi

我在使用C#中的XSL transromation合并多个文件时遇到问题。我跟随了几个来自sevreal网站的例子和帖子,我很接近,但没有乐趣。我在运行代码时遇到一个错误,该代码声明“在添加属性节点之后,无法将命名空间节点添加到父元素。”

这是到目前为止我的代码

string[] filesInCurrentDir = Directory.GetFiles(dirsWithTestCases[a], "*.xml");
string[] xslFilePath = Directory.GetFiles(dirsWithTestCases[a], "*.xsl");
string[] outputPathArray = Directory.GetFiles(dirsWithTestCases[a] + "\\Expected");
var resolver = new XmlPreloadedResolver();
var inputPath = string.Empty;    
for (int x = 0; x < filesInCurrentDir.Length; x++)
{
    if (filesInCurrentDir[x].IndexOf(".xml") > 0)
    {
        // First file load into doc variable
        if (inputPath == string.Empty)
        {
            // Load the first XML file into a document
            //input.LoadXml(filesInCurrentDir[x]);
            inputPath = filesInCurrentDir[x];
        }
        //Any and all other files
        else
        {
            var files = new FileInfo(filesInCurrentDir[x]);
            var inputStream = files.OpenRead();
            resolver.Add(new Uri(filesInCurrentDir[x]), inputStream);
        }
    }
}
// Load the XSL
var xslt = new XslCompiledTransform(true);
var xlstFile = new FileInfo(xslFilePath[0]);
var xsltDoc = new XmlDocument();
// Load the XSLT file through XmlReader to override the base URI.
using (StreamReader reader = File.OpenText(xslFilePath[0]))
using (XmlReader xmlReader = XmlReader.Create(reader, null, xslFilePath[0]))
{
    xsltDoc.Load(xmlReader);
}
// Use XsltSettings to enable the use of the document() function.
xslt.Load(xsltDoc, new XsltSettings(true, false), null);

// Define the output
var output = new FileInfo(outputPathArray[0] + ".UPDATED");

// Create input stream for first file
var inputFile = new FileInfo(inputPath);
//var inputStream = inputFile.OpenRead();
var input = new XmlDocument();
input.Load(inputPath);

// Compile stylesheet
var processor = new Processor();
var compiler = processor.NewXsltCompiler();
var executable = compiler.Compile(new Uri(xlstFile.FullName));

using (StringWriter OutputWriter = new StringWriter())
using (XmlWriter xmlWriter = XmlWriter.Create(OutputWriter))
{
    // Pass the resolver to the transform

    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true
    };
    var inputUri = new Uri(inputPath);
    xslt.Transform(input, xmlWriter);
}
错误:

“在已添加属性节点后,无法将命名空间节点添加到父元素。”

堆栈跟踪

at System.Xml.Xsl.Runtime.XmlQueryOutput.StartCopy(XPathNavigator navigator, Boolean callChk)
at System.Xml.Xsl.Runtime.XmlQueryOutput.CopyNode(XPathNavigator navigator)
at System.Xml.Xsl.Runtime.XmlQueryOutput.WriteItem(XPathItem item)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:template match="*" mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last, IList`1 {urn:schemas-microsoft-com:xslt-debug}namespaces)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.<xsl:template match="*" mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last, IList`1 {urn:schemas-microsoft-com:xslt-debug}namespaces)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.__ARR_AddPassengerInfoToBooking(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) in :line 0
at System.Xml.Xsl.CompiledQuery.Query.Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)
at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XmlWriter results)
at XSLTRegressionSuite.Program.Main(String[] args) in \XSLT.Regression.Suite\XSLTRegressionSuite\Program.cs:line 245
位于System.Xml.Xsl.Runtime.XmlQueryOutput.StartCopy(XPathNavigator navigator,布尔callChk)
位于System.Xml.Xsl.Runtime.XmlQueryOutput.CopyNode(XPathNavigator navigator)
位于System.Xml.Xsl.Runtime.XmlQueryOutput.WriteItem(XPathItem项)
在System.Xml.Xsl.CompiledQuery.Query.(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时,XPathNavigator{urn:schemas microsoft com:xslt debug}当前,双{urn:schemas microsoft com:xslt debug}位置,双{urn urn:schemas microsoft com:xslt debug}最后,IList`1{urn:schemas microsoft com:xslt debug}命名空间)
在System.Xml.Xsl.CompiledQuery.Query.(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时,XPathNavigator,双精度,双精度)
在System.Xml.Xsl.CompiledQuery.Query.(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时,XPathNavigator{urn:schemas microsoft com:xslt debug}当前,双{urn:schemas microsoft com:xslt debug}位置,双{urn urn:schemas microsoft com:xslt debug}最后,IList`1{urn:schemas microsoft com:xslt debug}命名空间)
在System.Xml.Xsl.CompiledQuery.Query.(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时,XPathNavigator,双精度,双精度)
在System.Xml.Xsl.CompiledQuery.Query.\uu ARR\u AddPassengerInfoToBooking(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时)的第0行
在System.Xml.Xsl.CompiledQuery.Query.Execute(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时)
在System.Xml.Xsl.XmlILCommand.Execute(对象defaultDocument、XmlResolver数据源、XslTargetList argumentList、XmlSequenceWriter结果)
在System.Xml.Xsl.XmlILCommand.Execute(对象defaultDocument、XmlResolver数据源、XslTargetList argumentList、XmlWriter)
在System.Xml.Xsl.xslcomiledTransform.Transform(IXPathNavigable输入、XSLTargetList参数、XmlWriter结果、XmlResolver documentResolver)
在System.Xml.Xsl.xslcomiledTransform.Transform(IXPathNavigatable输入,XmlWriter结果)
位于\XSLT.Regression.Suite\XSLTRegressionSuite\Program.cs中的XSLTRegressionSuite.Program.Main(字符串[]args):第245行
非常感谢您的帮助


干杯

问题出在您的XSLT代码中,您还没有向我们展示。嗨,Michael,谢谢您的回复。我正试图调试XSLT,看看这是否是问题所在,但我真的不知道如何去做。我知道VS2017有这个功能,但是调试XSLT按钮变灰了。这些XSLT是由Contivo Analyst生成的,因此它们不是手工编写的。我不知道我是否可以透露,因为它们属于我的客户,但我想我可以直接发送给你让你查看?对不起,这不是它的工作方式。如果你想要付费的XSLT咨询服务,我相信会有一些优秀的人来接手,但不会是我。当然,我理解。我将看看我的客户机是否允许我在这里发布XSLT和XML消息。现在谢谢你。
at System.Xml.Xsl.Runtime.XmlQueryOutput.StartCopy(XPathNavigator navigator, Boolean callChk)
at System.Xml.Xsl.Runtime.XmlQueryOutput.CopyNode(XPathNavigator navigator)
at System.Xml.Xsl.Runtime.XmlQueryOutput.WriteItem(XPathItem item)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:template match="*" mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last, IList`1 {urn:schemas-microsoft-com:xslt-debug}namespaces)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.<xsl:template match="*" mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last, IList`1 {urn:schemas-microsoft-com:xslt-debug}namespaces)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates mode="elaborate">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.__ARR_AddPassengerInfoToBooking(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) in :line 0
at System.Xml.Xsl.CompiledQuery.Query.Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)
at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XmlWriter results)
at XSLTRegressionSuite.Program.Main(String[] args) in \XSLT.Regression.Suite\XSLTRegressionSuite\Program.cs:line 245