OK!这个让我陷入困境。
我有一个包含多个条目的XML文件,我想通过1个元素的内部文本将其放入某些Gridview中。这是我的代码;
<asp:GridView ID="TicketViewURG" CssClass="table table-striped table-hover" runat="server" AutoGenerateColumns="false" OnRowDataBound="TicketView_RowDataBound" OnSorted="TicketViewURG_Sorted" Width="100%">
<Columns>
<asp:BoundField DataField="Time.Date" HeaderText="Created" />
<asp:BoundField DataField="Reference" HeaderText="Ref No." />
<asp:BoundField DataField="StoreID" HeaderText="Store I.D" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="completeStatus" HeaderText="Status" />
<asp:BoundField DataField="ActionRequired" HeaderText="ActionRequired" />
</Columns>
</asp:GridView>
<asp:GridView ID="TicketViewASAP" CssClass="table table-striped table-hover" runat="server" AutoGenerateColumns="false" OnRowDataBound="TicketView_RowDataBound" OnSorted="TicketViewURG_Sorted" Width="100%">
<Columns>
<asp:BoundField DataField="Time.Date" HeaderText="Created" />
<asp:BoundField DataField="Reference" HeaderText="Ref No." />
<asp:BoundField DataField="StoreID" HeaderText="Store I.D" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="completeStatus" HeaderText="Status" />
<asp:BoundField DataField="ActionRequired" HeaderText="ActionRequired" />
</Columns>
</asp:GridView>
<asp:GridView ID="TicketViewTODO" CssClass="table table-striped table-hover" runat="server" AutoGenerateColumns="false" OnRowDataBound="TicketView_RowDataBound" OnSorted="TicketViewURG_Sorted" Width="100%">
<Columns>
<asp:BoundField DataField="Time.Date" HeaderText="Created" />
<asp:BoundField DataField="Reference" HeaderText="Ref No." />
<asp:BoundField DataField="StoreID" HeaderText="Store I.D" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="completeStatus" HeaderText="Status" />
<asp:BoundField DataField="ActionRequired" HeaderText="ActionRequired" />
</Columns>
这就是我的XML样子:
<?xml version="1.0" encoding="utf-8"?>
<SupportTickets>
<Ticket Time.Date="24/05/2016 08:32:53">
<Reference>SAN3553</Reference>
<StoreID>Test</StoreID>
<Contact>Joanne</Contact>
<Department>Shop</Department>
<Description>TEST</Description>
<ActionRequired>ASAP</ActionRequired>
<completeStatus>Not Complete</completeStatus>
<ID>1</ID>
</Ticket>
<Ticket Time.Date="24/05/2016 08:32:53">
<Reference>SAN3553</Reference>
<StoreID>Test</StoreID>
<Contact>Joanne</Contact>
<Department>Shop</Department>
<Description>TEST. </Description>
<ActionRequired>TODO</ActionRequired>
<completeStatus>Not Complete</completeStatus>
<ID>2</ID>
</Ticket>
<Ticket Time.Date="24/05/2016 08:32:53">
<Reference>SAN3553</Reference>
<StoreID>Test</StoreID>
<Contact>Joanne</Contact>
<Department>Shop</Department>
<Description>TILL HAS STOPPED SCANNING NHS BARCODE. </Description>
<ActionRequired>ASAP</ActionRequired>
<completeStatus>Not Complete</completeStatus>
<ID>3</ID>
</Ticket>
</SupportTickets>
我想从XML中提取绑定字段名称,但将它们排序到正确的GridView,
因此,我的XML中的Entry No.1将转到TicketViewURG,因为XML元素ActionRequired的内部文本是ASAP。
我尝试使用的XML元素是<ActionRequired></ActionRequired>
答案 0 :(得分:0)
创建了一个小方法,可以满足您的需要。要获取不同网格视图的数据,只需通过<Ticket>
元素的索引:
List<Data> gridView1DS = this.GetData(doc,0);
List<Data> gridView2DS = this.GetData(doc,1);
List<Data> gridView3DS = this.GetData(doc,3);
代码背后:
public partial class BindXmlToGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XDocument doc = this.LoadXmlDoc("input.xml");
List<Data> gridView1DS = this.GetData(doc, 0);
TicketViewURG.DataSource = gridView1DS;
TicketViewURG.DataBind();
}
private List<Data> GetData(XDocument doc,int ticketIndex)
{
var list = new List<Data>();
var d = new Data();
XElement element = doc.Elements("SupportTickets").Elements().ElementAt(ticketIndex);
d.TimeDate = element.Attribute("Time.Date").Value;
d.Reference = element.Element("Reference").Value;
d.StoreID = element.Element("StoreID").Value;
d.Department = element.Element("Department").Value;
d.completeStatus = element.Element("completeStatus").Value;
d.ActionRequired = element.Element("ActionRequired").Value;
list.Add(d);
return list;
}
private XDocument LoadXmlDoc(string fileName)
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("~/App_Data/" + fileName));
return xmlDoc;
}
}
public class Data
{
public string TimeDate { get; set; }
public string Reference { get; set; }
public string StoreID { get; set; }
public string Contact { get; set; }
public string Department { get; set; }
public string Description { get; set; }
public string ActionRequired { get; set; }
public string completeStatus { get; set; }
public string ID { get; set; }
}
<强> .ASPX:强>
<asp:GridView ID="TicketViewURG" CssClass="table table-striped table-hover" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="TimeDate" HeaderText="Created" />
<asp:BoundField DataField="Reference" HeaderText="Ref No." />
<asp:BoundField DataField="StoreID" HeaderText="Store I.D" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="completeStatus" HeaderText="Status" />
<asp:BoundField DataField="ActionRequired" HeaderText="ActionRequired" />
</Columns>
</asp:GridView>