1

我目前正在处理一个 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"));
    }
}

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>
4

1 回答 1

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();
        }
    }
于 2016-01-25T16:27:02.527 回答