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 ファイルに間違いがあったためなのかはわかりません。以下のコードを確認してください。

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

1 に答える 1

0

コメントで述べたように、カスタム デシリアライザーが XML データをオブジェクトに解析する方法がわかりません。Linq-to-XMLを使用するIMHOは、ここでは非常に単純であり、複雑さを伴わずに簡単に進むことができます:-

 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 に答える