在Nustache中使用XPath函数



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

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

使用类似{{sum(ítems/price)}}的东西会非常有用

您可以尝试这样做:

控制器:

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>

希望这能有所帮助!

最新更新