我目前正在开发一个ASP.NET Web Forms项目,我需要使用嵌套转发器为Job Postings页面显示一些反序列化的XML数据。
我遇到的问题是嵌套转发器只显示第一个XMLElement项,而不显示XMLAttribute中作为元素一部分的数据。
例如,XML数据如下所示:
Jobs.xml
[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"));
}
}
我想要完成的是让第一个转发器循环通过“作业”并显示类别,标题,描述等。然后在嵌套转发器中显示作业位置及其工资。
因此,例如从上面显示的XML数据中得出的结果如下:
然后对于同一个工作,再次显示数据,但显示其他位置和工资......
目前,嵌套转发器仅显示第一个作业位置,然后没有显示工资,然后转到下一个作业,而不显示同一作业的其他位置和工资。我不确定这是因为我在.aspx文件中使用转发器的结构所犯的错误,还是因为我用于反序列化XML数据的.cs文件中的错误。请查看下面提供的代码。
Jobs.cs
<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>
JobCategories.aspx
{{1}}
答案 0 :(得分:0)
正如评论中所提到的,我不确定您的自定义反序列化器如何将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();
}
}