在最近的一个项目中,我能够使用以下语法从我的 XML 文件中获取不同的值:
<xsl:for-each select="distinct-values($ds/datasource/Products-list/Products/CategoryName)">
但是现在,我们正在将项目迁移到 ASP.NET 并且以下代码不起作用:
public DataSourceManager manager = new DataSourceManager();
protected void Page_Load(object sender, EventArgs e)
{
this.manager.Get("http:***",
"distinct-values(/datasource/Products-list/Products/CategoryName)",
new String[] { "." }, this.messageRepeater);
}
}
Get 函数如下所示:
public void Get(String datasourceUrl,
String xpathToNodes,
Array nodeNames,
Repeater repeater,
params String[] options ) {
Debug.WriteLine("datasourceUrl= " + datasourceUrl);
Debug.WriteLine("xpathToNodes= " + xpathToNodes);
//call datasource url
XmlDocument doc = new XmlDocument();
doc.Load(datasourceUrl);
//statusCode
this.statusCode = doc.SelectSingleNode("/datasource/result/status/@code").Value;
if (options.GetLength(0) > 0) {
this.maxItem = Convert.ToInt16(options[0]);
}
//iterate
this.list = new ArrayList();
int count = 0;
if (IsErrorCode == false) {
XmlNodeList nodes = doc.SelectNodes(xpathToNodes);
foreach (XmlNode node in nodes) {
Hashtable row = new Hashtable();
foreach (String nodeName in nodeNames) {
row.Add(nodeName, node.SelectSingleNode(nodeName).InnerText);
}
list.Add(row);
if (++count == this.maxItem) {
break;
}
}
}
//data binding
repeater.DataSource = list;
repeater.DataBind();
}
返回的错误如下:
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
如何在 ASP.NET 中获取不同的值?
最佳答案
我相信 distinct-values
函数只在 xslt 2.0 中可用。如果您使用 .NET 内置的 XSLT 功能做任何事情,它只支持 1.0。这可以解释您的错误。
不幸的是,在 XSLT 1.0 上选择不同的记录可能有点麻烦。这篇 SO 帖子介绍了执行此操作的好方法:
关于c# - 获得不同的值(value);网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7324642/