【问题标题】:How to create next and previous for a details page using c# and an id in XML?如何使用 c# 和 XML 中的 id 为详细信息页面创建下一个和上一个?
【发布时间】:2017-10-24 13:29:11
【问题描述】:

我有这个 XML,我正忙于一个广告,我创建了两个页面,一个用于我的 XML 中的所有产品,一个是每个产品的详细信息页面。每个产品都有一个这样的 URL(“details.aspx?uId=3332”)。我想在详细信息页面中创建两个按钮(下一个和上一个),这样当您单击下一个时,它将查看 xml 并找到下一个带有 ulId 的产品,该产品紧随正在显示的产品之后或(到将 url 更改为 details.aspx?uId=(下一个要查找的 uId (在本例中为 3333)))。

<?xml version="1.0" encoding="utf-8" ?>
    <root>
        <advertentie>
        <uId>3332</uId>
        <auto>
            <merk>GX-TM</merk>
            <carroserie>Sedan</carroserie>
        </auto>
        <afbeeldingen>
            <laatsUpdate>10.00</laatsUpdate>
  <FotoGroot>http://etc.usf.edu/clipart/63300/63366/63366_head_lg.gif</FotoGroot>
  <FotoGroot>https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRsXe7qqTisxV9_XpRGtY64fWMllpSAZwSIDzqRMLp4EVgJwStO</FotoGroot>
</afbeeldingen>
</advertentie>
 <advertentie>
<uId>3333</uId>
<auto>
  <merk>MB-Maybach</merk>
  <carroserie>Sedan</carroserie>
</auto>
<afbeeldingen>
  <laatsUpdate>11.00</laatsUpdate>
  <FotoGroot>http://static.robbreport.com/sites/default/files/mercdes-s600-maybach.jpg</FotoGroot>
  <FotoGroot>https://images.cdn.autocar.co.uk/sites/autocar.co.uk/files/styles/gallery_slide/public/merc-s-class-road-test-017.jpg?itok=pF_TCkMU</FotoGroot>
</afbeeldingen>
</advertentie>
<advertentie>
<uId>3334</uId>
<auto>
  <merk>Royal-Gold</merk>
  <carroserie>Stationwagen</carroserie>
</auto>
<afbeeldingen>
  <laatsUpdate>12.00</laatsUpdate>
  <FotoGroot>http://iluvesports.com/wp-content/uploads/2016/07/royal-car-collection-1-600x369.jpg</FotoGroot>
  <FotoGroot>https://i.ytimg.com/vi/RDe3ZvTv5AQ/maxresdefault.jpg</FotoGroot>
</afbeeldingen>
</advertentie>
</root>

【问题讨论】:

  • 点击按钮时需要通过ajax获取nextId还是生成页面时可用?我认为最好只解析一次 XML,获取当前产品的信息和两个 id,下一个和上一个,然后将它们传递到页面。
  • @Atchitutchuk 我添加了 ajax 和 javascript,因为我做了一些研究,许多答案都是基于 ajax 和/的 javascript,但对我来说,我愿意接受任何结果。

标签: javascript c# asp.net ajax xml


【解决方案1】:

试试这个

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;




namespace WindowsFormsApplication29
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\test.xml";
        List<XElement> advertentie = null;
        int advertentieIndex = 0;
        public Form1()
        {
            InitializeComponent();

            XDocument doc = XDocument.Load(FILENAME);

            advertentie = doc.Descendants("advertentie").ToList();
            textBoxUserId.Text = (string)advertentie[advertentieIndex].Element("uId");
        }



        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            if (advertentieIndex != 0)
            {
                advertentieIndex -= 1;
                textBoxUserId.Text = (string)advertentie[advertentieIndex].Element("uId");
            }

        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (advertentieIndex != advertentie.Count - 1)
            {
                advertentieIndex += 1;
                textBoxUserId.Text = (string)advertentie[advertentieIndex].Element("uId");
            }

        }



    }
}

【讨论】:

  • 我看到你在这里做了什么,但我发布的 xml 是我的 intire 项目的一部分(演示),我有几个 uId,比如 8562、7745 等等,我该怎么做这样做?
  • 在advertentie下查看下一个uId不是更好吗(只是从初学者的角度来说)?
  • 代码查找所有标签 advertentie (descendants) 并将它们放入列表中。 UserID 是 advertentie 的孩子。我发布的代码显示了所有三个 ID。
  • 我以这种方式编写代码有两个原因 1) Element("uid") 将搜索所有子元素,因此顺序并不重要(不喜欢 next 取决于顺序)。 2) 使用 "Descendants("Advertentie") 允许您获取用户 ID 和所有其他属性。不仅仅是用户 ID。
  • 好的谢谢我去试试,也谢谢你的解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 2023-04-08
  • 1970-01-01
相关资源
最近更新 更多