迭代xslt中的字符串列表,并使用索引

时间:2016-10-10 15:33:08

标签: c# xslt xml-serialization

我在C#工作。 我使用XSL转换(XSLT)版本1.0。 https://www.w3.org/TR/xslt 我使用xslt来从MyClass类中的数据创建xml MyClass与模板匹配并具有属性

canvas.addEventListener("mousemove", function (e) {
  console.log("mousemove");
}, false);
canvas.addEventListener("mouseup", function (e) {
  console.log("mouseup");
}, false);

我需要这样导出:

 List<string> Strings

有人可以帮我实现如何做吗? 祝一切顺利 塔尔

2 个答案:

答案 0 :(得分:1)

很简单:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication16
{
    class Program
    {

        static void Main(string[] args)
        {
            List<string> Strings = new List<string>() {
                    "first string value Here",
                    "Second string value here"
            };

            List<XElement> output = Strings.Select((x, i) => new XElement("Line" + (i + 1).ToString(), x)).ToList(); 
        }

    }

}

答案 1 :(得分:0)

我找到了解决方案。也许有更好的...... 我创建了一个新的ExportClass:

public class ExportString
{
   [XmlElement("StringObject")]
   public string StringObject{get; set;}
}

我创建了一个列表,因为属性是与XSLT匹配的MyExportClass:

public class MyExportClass
{
   [XmlElement("LOS"]
   public List<StringObject> LOS<get; set;}
}

我在XSLT中使用了这种语法: 代码包含元素名称中的索引:

<xsl:for-each select="LOS" xml:space="default">

 //Get the index
  <xsl:variable name ="index" select="position()"/>

  <xsl:element name ="Line{$index}">
  <xsl:value-of select = "StringObject"/>
  </xsl:element>

   //Line break
  <xsl:text> &#xa;</xsl:text>
</xsl:for-each>

结果:

<Line1> my first string </Line1>
<Line2> my second string </Line2>
.
.
.
相关问题