在Nustache中使用XPath函数

时间:2017-08-30 19:50:50

标签: c# xml mustache nustache

在Nustache(Mustache for C#)中,您可以使用XML文档作为渲染数据,然后可以使用返回模板中节点的XPath表达式,如{{parent/child/node}}

是否可以在模板中使用XPath函数?

使用像{{sum(ítems/price)}}

这样的东西非常有用

1 个答案:

答案 0 :(得分:0)

您可以尝试这样做:

<强>控制器:

public ActionResult Index()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Price");

    // Using HtmlAgilityPack Library
    HtmlDocument doc = new HtmlDocument();

    doc.Load("FILE_PATH_TO_YOUR_XML_FILE");

    // Get the XPath Value
    string xPath_Price = "price";

    // Now get all of the prices

    foreach (HtmlNode price in doc.DocumentNode.SelectNodes("//price"))
    {
         DataRow dr = dt.NewRow();

         dr["Price"] = price.SelectSingleNode(xPath_Price).InnerText;
         dt.Rows.Add(dr);
    }

    // Now Serialize this data and store it as a string
    // Use Newtonsoft.Json Library
    string all_prices = JsonConvert.SerializeObject(dt);

    // Now use this string and store it as a ViewBag Object to use in your View

    ViewBag.XPathPrices = all_prices;

    return View();

}

查看:

<div class="PricesContainer">
    @ViewBag.XPathPrices
</div>

希望这有帮助!