【发布时间】:2014-10-09 01:44:05
【问题描述】:
我有一个使用 XmlDocument 类加载的非常简单的 xml 字符串。现在我试图通过 XPath 查询读取它,我收到了这个错误,
“表达式必须计算为节点集。”
这是我的 Xml,
<RF_SearchTermBanners>
<ImageName>3pc-leather-set.jpg</ImageName>
</RF_SearchTermBanners>
这是我的 C# 代码,
protected void BindSearchBanner(string ImageUrl)
{
//Parse Xml string containing Image name
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(ImageUrl);
System.Xml.XmlNode node = (System.Xml.XmlNode)xmlDoc.DocumentElement;
System.Xml.XmlElement imageElem = node.SelectSingleNode("@/RF_SearchTermBanners/ImageName") as System.Xml.XmlElement;
string imgUrl = imageElem.InnerText;
if (imgUrl != null && imgUrl != string.Empty)
{
SearchBanner.ImageUrl = "~/Themes/Default/Images" + imgUrl;
SearchBanner.Visible = true;
}
else
{
SearchBanner.ImageUrl = string.Empty;
SearchBanner.Visible = false;
}
}
请帮忙。
【问题讨论】:
-
代码 LoadXml(ImagePath) 看起来很奇怪。您确定将 XML 文本传递给 LoadXml 方法吗?看起来你正在传递一个 Url,但它不是那样工作的......