传递XSLT参数列表中的对象列表并在C#中以xsl访问它

我有一个我在XSL中使用的类

public class PurchaseOrderDTO
{
    public IList<PurchaseOrderDetailsDTO> PurchaseOrderDetailsDTOList { get; set; }
    public String PONo { get; set; }
    public Decimal POValue { get; set; }
    public DateTime? PODate { get; set; }
}

以下是我使用的xsl
    

<P>
  Dear,
</P>
Purchase Order No <xsl:value-of select="PurchaseOrderDTO:get_PONo()" /> has
being created <BR />
Purchase Order Value <xsl:value-of select="PurchaseOrderDTO:get_POValue()" /> <BR />
<BR />
Purchase Order Date <xsl:value-of select="PurchaseOrderDTO:get_PODate()" /> <BR />
<BR />

<xsl:for-each select="PurchaseOrderDTO:get_PurchaseOrderDetailsDTOList()">
  <ul>
    <li>
      <p>      
        Item name
      </p>
      <xsl:value-of select="PurchaseOrderDetailsDTO:get_Item()"></xsl:value-of>
    </li>
  </ul>
</xsl:for-each>
  private static PurchaseOrderDTO GetPurchaseOrderDTO()
    {
        PurchaseOrderDTO _PurchaseOrderDTO = new PurchaseOrderDTO();
        Random rand = new Random();

        _PurchaseOrderDTO.PODate = DateTime.Now;
        _PurchaseOrderDTO.PONo = "PO3333";
        _PurchaseOrderDTO.POValue = rand.Next(40200, 960000);

        IList<PurchaseOrderDetailsDTO> _PurchaseOrderDetailsDTOList = new List<PurchaseOrderDetailsDTO>();

        for (int i = 1; i < 3; i++)
        {
            PurchaseOrderDetailsDTO _PurchaseOrderDetailsDTO = new PurchaseOrderDetailsDTO();
            _PurchaseOrderDetailsDTO.ID = (Int16)i;
            _PurchaseOrderDetailsDTO.Item = "Item" + i;
            _PurchaseOrderDetailsDTO.Value = Convert.ToDecimal(rand.Next(100, 500) * i);
            _PurchaseOrderDetailsDTOList.Add(_PurchaseOrderDetailsDTO);
        }

        _PurchaseOrderDTO.PurchaseOrderDetailsDTOList = _PurchaseOrderDetailsDTOList;

        return _PurchaseOrderDTO;


    }

    private static void GetEmailTemplate()
    {
        PurchaseOrderDTO _PurchaseOrderDTO = GetPurchaseOrderDTO();

        XslTransform _XslTransform = new XslTransform();
        _XslTransform.Load("../../Transformer.xslt");

        XmlDocument _XmlDocument = new XmlDocument();
        _XmlDocument.AppendChild(_XmlDocument.CreateElement("Root"));

        XPathNavigator _XPathNavigator = _XmlDocument.CreateNavigator();

        XsltArgumentList _XsltArgumentList = new XsltArgumentList();


        Hashtable _Hashtable = new Hashtable();
        _Hashtable["PurchaseOrderDTO"] = _PurchaseOrderDTO;

        if (_Hashtable != null)
        {
            foreach (DictionaryEntry entry in _Hashtable)
            {
                _XsltArgumentList.AddExtensionObject(entry.Key.ToString(), entry.Value);
            }
        }

        StringBuilder _sb = new StringBuilder();
        XmlTextWriter _XmlTextWriter = new XmlTextWriter(new StringWriter(_sb));

        _XslTransform.Transform(_XPathNavigator, _XsltArgumentList, _XmlTextWriter);

        Console.WriteLine(_sb.ToString());



    }

但是在输出中我没有获得子属性值.是否可以访问XsltArgumentList中的列表项值?

最佳答案
我不认为是,我不敢.玩弄你的代码我得到错误告诉我不支持List’1的返回类型(即详细对象的集合)

检查XSLT extension objects documentation告诉我这个:

The data types returned from extension
objects are one of the four basic
XPath data types of number, string,
Boolean, and node set

哪种方式明确表示不会以您尝试使用它们的方式支持任意CLR对象.

可能值得以稍微不同的方式处理问题 – 您希望将CLR对象与这样的模板合并,然后预编译的T4模板可能是更好的选择.我在这里有一篇博客文章描述:Using T4 to generate content at runtime

转载注明原文:传递XSLT参数列表中的对象列表并在C#中以xsl访问它 - 代码日志