简体   繁体   English

如何下载 XML 元素作为 GridView (asp.net) 的链接?

[英]How to download XML element as a link to GridView (asp.net)?

I need to display data ("document" and "manual" XML elements) from XML file into GridView control as a link.我需要将 XML 文件中的数据(“文档”和“手册”XML 元素)作为链接显示到 GridView 控件中。 The only way that works for me is using CDATA section (I used it for "document" element below).对我有用的唯一方法是使用 CDATA 部分(我将它用于下面的“文档”元素)。 But I need to find a way to avoid using CDATA to make.xml file simpler.但我需要找到一种方法来避免使用 CDATA 使.xml 文件更简单。 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.它应该允许我在文件后面的代码中将链接作为字符串,并将文档的名称留在.xml 文件中。 I tried another way with "manual" element (using XmlNode).我尝试了另一种使用“手动”元素的方法(使用 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.但问题是它在 GridView 的所有单元格中显示相同的链接(因为它从所有 XML 文档中获取“手动”元素),但每一行应该不同。

Please see my .xml file, .cs file and .aspx page below.请参阅下面的.xml文件、 .cs文件和.aspx页面。

XML file: XML 文件:

<?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): .aspx 页面(部分带有 GridView 代码):

<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.我使用 C# 来读取 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);
                    }
                }
            }
        }
    }
}

I put results into a datatable.我将结果放入数据表中。 You can modify and put into any object您可以修改并放入任何 object

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq;
using System.Data;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Topic Name", typeof(string));
            dt.Columns.Add("Manual", typeof(string));
            dt.Columns.Add("Project", typeof(string));
            dt.Columns.Add("Document", typeof(string));
            dt.Columns.Add("HREF", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);
            string pattern = "href=\"(?'href'[^\"]+)\">(?'innertext'[^<]+)";
            foreach (XElement topic in doc.Descendants("topic"))
            {
                string name = (string)topic.Element("name");
                string manual = string.Join(",", topic.Elements("manual").Select(x => ((string)x).Trim()));
                string project = string.Join(",",topic.Elements("project").Select(x => ((string)x).Trim()));

                XElement document = topic.Element("document");

                string items = document.Value;
                MatchCollection matches = Regex.Matches(items, pattern);
                foreach (Match match in matches)
                {
                    dt.Rows.Add(new object[] {
                        name,
                        manual,
                        project,
                        match.Groups["innertext"].Value,
                        match.Groups["href"].Value
                    });
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM