I need to display data ("document" and "manual" XML elements) from XML file into GridView control as a link. The only way that works for me is using CDATA section (I used it for "document" element below). But I need to find a way to avoid using CDATA to make .xml file simpler. It should allow me to make a link as a string in code behind file and leave just the name of the document in .xml file. I tried another way with "manual" element (using XmlNode). But the problem is that it display the same links in all the cells of GridView (because it takes "manual" elements from all the XML document) but it should be different for each row.
Please see my .xml
file, .cs
file and .aspx
page below.
XML file:
<?xml version="1.0" encoding="utf-8" ?>
<document_topics>
<release id="936896294">
<topic>
<name>
Account Connect
</name>
<document>
<![CDATA[<a href="DocumentLocator.aspx?dn=AT100000&r=936896294">AT100000</a><br>
<a href="DocumentLocator.aspx?dn=AT100010&r=936896294">AT100010</a><br>
<a href="DocumentLocator.aspx?dn=AT100020&r=936896294">AT100020</a><br>
<a href="DocumentLocator.aspx?dn=AT100030&r=936896294">AT100030</a><br>
<a href="DocumentLocator.aspx?dn=AT100040&r=936896294">AT100040</a>]]>
</document>
<manual>
RR320000
</manual>
<project>
</project>
</topic>
<topic>
<name>
Bankruptcy Proof of Claim
</name>
<document>
<![CDATA[<a href="DocumentLocator.aspx?dn=PM400692&r=936896294">PM400692</a><br>
<a href="DocumentLocator.aspx?dn=RP1212001&r=936896294">RP1212001</a><br>
<a href="DocumentLocator.aspx?dn=RP1212002&r=936896294">RP1212002</a><br>
<a href="DocumentLocator.aspx?dn=SP027POCF&r=936896294">SP027POCF</a><br>
<a href="DocumentLocator.aspx?dn=SP027POCL&r=936896294">SP027POCL</a><br>]]>
</document>
<manual>
TR320010
</manual>
<manual>
TR320020
</manual>
<project>
PD55588
</project>
<project>
PD23459
</project>
</topic>
</release>
</document_topics>
`
.aspx page (part with GridView code):
<asp:GridView ID="gvDocumentTopics" SkinID="MainGrid" EmptyDataText="No Results" Width="100%" runat="server">
<Columns>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="Topic">
<ItemTemplate>
<%# XPath("name").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="Document Reference">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="" Text='<%# XPath("document").ToString() %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="Manual Reference">
<ItemTemplate>
<%foreach (var link in links)
{%>
<%=link%><br />
<%}%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="Related Project">
<ItemTemplate>
<%# XPath("project").ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
I used C# for reading XML.
using Latitude.Web.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace Latitude.Web
{
public partial class Index : System.Web.UI.Page
{
public List<string> links = new List<string>();
public string link;
public string relId;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["relId"] != null)
{
relId = ValidationHelper.ValidateReleaseIdString(Request.QueryString["relId"], ExceptionHelper.GetValidReleaseException(Request.QueryString["relId"]));
XmlDataSource1.DataFile = Server.MapPath("~/App_Data/IndexItems.xml");
XmlDataSource1.XPath = "document_topics/release[@id='" + relId + "']/topic";
XmlDataSource1.DataBind();
gvDocumentTopics.DataSource = XmlDataSource1;
gvDocumentTopics.DataBind();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/App_Data/IndexItems.xml"));
XmlNodeList manualsXML = doc.SelectNodes("document_topics/release[@id='" + relId + "']/topic/manual");
foreach (XmlNode xNode in manualsXML)
{
string manual = xNode.InnerText;
if (!String.IsNullOrEmpty(manual))
{
string link = String.Format("<a href=\"DocumentLocator.aspx?dn={0}&r={1}\">{0}</a>", manual, relId);
links.Add(link);
}
}
}
}
}
}
` is not closed and `&` is not escaped in the `href` attribute. From there, use your own XML structure that doesn't repeat anything that doesn't change. If you need HTML at some point, reconstitute it from your XML structure. – madreflection Oct 08 '19 at 15:15