我目前在我的主页上使用一个过滤器,它允许您从下拉列表中选择一个类别,然后通过点击搜索显示客户名称和徽标。
我使用了以下链接中解释的教程:
http://www.dbtutorials.com/retrieve/linq-to-xml-cs.aspx
目前虽然我像这样在 XML 中手动输入超链接:
<ClientName><![CDATA[<a href='category-list.aspx?cat=exampleclient'>Example Client</a>]]></ClientName>
这确实正确显示了链接,但认为它有点混乱。
我更喜欢它,所以它看起来像:
<ClientName>Example Client</ClientName>
在 c# 中,我试图使超链接工作,但它并没有完全奏效。这是在获取 XML 信息的编辑之前:
litXMLData.Text = litXMLData.Text + person.ClientLogo + "<br />";
litXMLData.Text = litXMLData.Text + person.ClientName;
当我尝试更新它时:
litXMLData.Text = "<a href='category-list.aspx?cat=" + litXMLData.Text + person.ClientName + ""'>" + person.ClientName + "</a>";
并出现以下错误:
CS1010:常量换行
所以我有点卡住了......有什么想法吗?
如果在我上面的更新之前有帮助,下面是所有 C# 代码:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
loadDDL();
}
protected void loadDDL()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XML/homepage-results.xml"));
XmlNodeList nodeList = doc.SelectNodes("Persons/Person");
//Below code modified by Chris so duplicated categories are not displayed ;
foreach(XmlNode node in nodeList)
{
ListItem newItem = new ListItem(node.SelectSingleNode("category").InnerText);
if (!ddlcategory.Items.Contains(newItem))
{
ddlcategory.Items.Add(newItem);
}
}
}
//End of modification ;
protected void butGetXML_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("XML/homepage-results.xml"));
var persons = from person in xmlDoc.Descendants("Person")
select new
{
ClientLogo = person.Element("ClientLogo").Value,
category = person.Element("category").Value,
ClientName = person.Element("ClientName").Value,
};
litXMLData.Text = "";
foreach (var person in persons)
{
litXMLData.Text = litXMLData.Text + "" + person.ClientLogo + "<br />";
//Removed category name displaying on the screen - litXMLData.Text = litXMLData.Text + "Category: " + person.category + "<br />";
litXMLData.Text = litXMLData.Text + "" + person.ClientName + "<br /><br />";
}
if (litXMLData.Text == "")
litXMLData.Text = "No Results.";
}
protected void butFilterXML_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("XML/homepage-results.xml"));
var persons = from person in xmlDoc.Descendants("Person")
where person.Element("category").Value == ddlcategory.SelectedItem.ToString()
select new
{
ClientLogo = person.Element("ClientLogo").Value,
category = person.Element("category").Value,
ClientName = person.Element("ClientName").Value,
};
litXMLData.Text = "";
foreach (var person in persons)
{
litXMLData.Text = litXMLData.Text + person.ClientLogo + "<br />";
litXMLData.Text = litXMLData.Text + person.ClientName + "<br /><br />";
}
if (litXMLData.Text == "")
litXMLData.Text = "No Results.";
}
}