ASP.NET 嵌套转发器问题
本文关键字:问题 转发器 嵌套 NET ASP | 更新日期: 2023-09-27 18:31:44
我目前正在处理一个 ASP.NET Web 窗体项目,我需要使用职位发布页面的嵌套转发器显示一些反序列化的 XML 数据。
我遇到的问题是嵌套转发器只显示第一项,即 XMLElement,而不显示来自 XMLAttribute 的数据,这是元素的一部分。
例如,XML 数据如下所示:
工作.xml
<Jobs>
<Job Category="Administration" Title="Senior Human Resource Coordinator">
<Description>
<![CDATA[ Long description of the job goes here. ]]>
</Description>
<ShortDescription>
<![CDATA[ Short description of the job goes here. ]]>
</ShortDescription>
<JobLocations>
<Location Salary="$50000">Toronto</Location>
<Location Salary="£40000">London</Location>
</JobLocations>
<Benefits>Drug, Dental, Optical and Extended Healthcare Benefits</Benefits>
<Jobtype>Full-Time</Jobtype>
</Job>
<Job Category="Administration" Title="Junior Human Resource Coordinator">
<Description>
<![CDATA[ Long description of the job goes here. ]]>
</Description>
<ShortDescription>
<![CDATA[ Short description of the job goes here. ]]>
</ShortDescription>
<JobLocations>
<Location Salary="$50000">Toronto</Location>
<Location Salary="£40000">London</Location>
</JobLocations>
<Benefits>Drug, Dental, Optical and Extended Healthcare Benefits</Benefits>
<Jobtype>Full-Time</Jobtype>
</Job>
</Jobs>
我希望完成的是让第一个中继器循环浏览"作业"并显示类别、标题、描述等。并在嵌套中继器中显示工作地点及其工资。
因此,例如,从上面显示的XML数据来看,结果如下:
- 类别: 管理
- 职位:高级人力资源协调员
- 描述:。。。
- 简短描述: ...
- 地点: 多伦多
- 薪水:$ 50000
- 。
然后对于同一工作,再次显示数据,但显示其他位置和薪水...
- 类别: 管理
- 职位:高级人力资源协调员
- 描述:。。。
- 简短描述: ...
- 地点: 伦敦
- 薪资待遇:类别:行政
- 职位:高级人力资源协调员
- 描述:。。。
- 简短描述: ...
- 地点: 伦敦
- 薪水:£40000
目前,嵌套转发器仅显示第一个工作位置,然后不显示工资,然后转到下一个作业,而不显示同一作业的其他位置和工资。我不确定这是因为我在.aspx文件中的转发器结构上犯了一个错误,还是因为我用于反序列化 XML 数据的.cs文件中的错误。请查看下面提供的代码。
工作.cs
[Serializable]
public class Job
{
[XmlAttribute]
public string Category { get; set; }
[XmlAttribute]
public string Title { get; set; }
[XmlElement]
public string Description { get; set; }
[XmlElement]
public string ShortDescription { get; set; }
public string Benefits { get; set; }
[XmlElement]
public string Jobtype { get; set; }
[XmlElement("JobLocations")]
public List<JobLocation> JobLocations { get; set; }
public class JobLocation
{
[XmlElement("Location")]
public string Location { get; set; }
[XmlAttribute("Salary")]
public string Salary { get; set; }
}
public static List<Job> Load(string path)
{
return SerializerSupport.DeserializeList<Job>(System.IO.Path.Combine(path, "jobs.xml"));
}
}
工作类别.aspx
<div class="flex-container">
<div class="flex-row">
<!-- Category Repeater -->
<asp:Repeater ID="Job" runat="server" ItemType="COMPANY.Ecommerce.API.Models.Job" SelectMethod="JobGrid_GetData">
<ItemTemplate>
<div class="flex-item">
<div>Title: <%#Item.Title%></div>
<div>S.Desc: <%#Item.ShortDescription%></div>
<asp:Repeater runat="server" ItemType="COMPANY.Ecommerce.API.Models.Job+JobLocation" DataSource="<%#Item.JobLocations%>">
<ItemTemplate>
<div>Location: <%#Item.Location%></div>
<div>Salary: <%#Item.Salary%></div>
</ItemTemplate>
</asp:Repeater>
<div>
</div>
<div>Category: <%#Item.Category%></div>
<div>Jobtype: <%#Item.Jobtype%></div>
<div>Benefits: <%#Item.Benefits%></div>
<div>Desc: <%#Item.Description%></div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
如评论中所述,我不确定您的自定义反序列化程序如何将 XML 数据解析为对象。恕我直言,使用 Linq-to-XML 在这里非常简单,而且它直截了当,没有任何复杂性:-
public class Job
{
public string Category { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string ShortDescription { get; set; }
public string Benefits { get; set; }
public string Jobtype { get; set; }
public List<JobLocation> JobLocations { get; set; }
public class JobLocation
{
public string Location { get; set; }
public string Salary { get; set; }
}
public static List<Job> Load(string path)
{
static XDocument xdoc = XDocument.Load(path);
return xdoc.Descendants("Job")
.Select(x => new Job
{
Category = (string)x.Attribute("Category"),
Title = (string)x.Attribute("Title"),
Description = (string)x.Element("Description"),
ShortDescription = (string)x.Element("ShortDescription"),
Benefits = (string)x.Element("Benefits"),
Jobtype = (string)x.Element("Jobtype"),
JobLocations = x.Element("JobLocations").Elements("Location")
.Select(jl => new JobLocation
{
Location = (string)jl,
Salary = (string)jl.Attribute("Salary")
}).ToList()
}).ToList();
}
}