ASP.NET 嵌套中继器问题
ASP.NET Nested Repeater Issues
我目前正在开发一个 ASP.NET Web 窗体项目,我需要在招聘信息页面使用嵌套转发器显示一些反序列化的 XML 数据。
我遇到的问题是嵌套转发器只显示作为 XML 元素的第一项,而不显示作为元素一部分的 XML 属性的数据.
例如 XML 数据如下所示:
Jobs.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="000">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="000">Toronto</Location>
<Location Salary="£40000">London</Location>
</JobLocations>
<Benefits>Drug, Dental, Optical and Extended Healthcare Benefits</Benefits>
<Jobtype>Full-Time</Jobtype>
</Job>
</Jobs>
我想要完成的是让第一个中继器循环显示 "Job" 并显示类别、标题、描述等。并在嵌套的中继器中显示工作地点及其薪水。
例如,上面显示的 XML 数据的结果如下:
- 类别:管理
- 职位:高级人力资源协调员
- 描述:...
- 简短说明:...
- 地点:多伦多
- 工资:$50000
- ...
然后对于同一份工作,再次显示数据,但显示其他位置和薪水...
- 类别:管理
- 职位:高级人力资源协调员
- 描述:...
- 简短说明:...
- 地点:伦敦
- 薪资:类别:管理
- 职位:高级人力资源协调员
- 描述:...
- 简短说明:...
- 地点:伦敦
- 工资:40000英镑
目前,嵌套转发器只显示第一个工作地点,然后不显示薪水,然后转到下一个工作而不显示同一工作的其他地点和薪水。我不确定这是因为我在 .aspx 文件中对转发器的结构犯了错误,还是因为我用于反序列化 XML 数据的 .cs 文件中有错误。请查看下面提供的代码。
Jobs.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"));
}
}
JobCategories.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();
}
}
我目前正在开发一个 ASP.NET Web 窗体项目,我需要在招聘信息页面使用嵌套转发器显示一些反序列化的 XML 数据。
我遇到的问题是嵌套转发器只显示作为 XML 元素的第一项,而不显示作为元素一部分的 XML 属性的数据.
例如 XML 数据如下所示:
Jobs.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="000">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="000">Toronto</Location>
<Location Salary="£40000">London</Location>
</JobLocations>
<Benefits>Drug, Dental, Optical and Extended Healthcare Benefits</Benefits>
<Jobtype>Full-Time</Jobtype>
</Job>
</Jobs>
我想要完成的是让第一个中继器循环显示 "Job" 并显示类别、标题、描述等。并在嵌套的中继器中显示工作地点及其薪水。
例如,上面显示的 XML 数据的结果如下:
- 类别:管理
- 职位:高级人力资源协调员
- 描述:...
- 简短说明:...
- 地点:多伦多
- 工资:$50000
- ...
然后对于同一份工作,再次显示数据,但显示其他位置和薪水...
- 类别:管理
- 职位:高级人力资源协调员
- 描述:...
- 简短说明:...
- 地点:伦敦
- 薪资:类别:管理
- 职位:高级人力资源协调员
- 描述:...
- 简短说明:...
- 地点:伦敦
- 工资:40000英镑
目前,嵌套转发器只显示第一个工作地点,然后不显示薪水,然后转到下一个工作而不显示同一工作的其他地点和薪水。我不确定这是因为我在 .aspx 文件中对转发器的结构犯了错误,还是因为我用于反序列化 XML 数据的 .cs 文件中有错误。请查看下面提供的代码。
Jobs.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"));
}
}
JobCategories.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();
}
}