Thread: URL Request

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Western Isle of Scotland
    Posts
    258
    Rep Power
    17

    URL Request


    Can you help me out, I'm new to C#.

    I'm load an XML doc into a webpage using C# and I'm wondering how I can load specific IDs from my XML doc. i.e

    Default.aspx?ID=2008 will load 2008 data

    here's my code so far

    XML doc
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <mainTemplate>
    
      <item ID="2008">
    
        <type >video</type>
        <thumb>thumbs/2008/01.jpg</thumb>
        <main>video/videos2008/2008 01.flv</main>
        <title>An Iar Chuairt Dheireannach</title>
        <description><![CDATA[<b>Josie Burgess</b> - Airson]]></description>
        <school>Sgoil Lonacleit</school>
        <topic>Bu chir do dh'Albha bhith seasamh leatha fhin aig na h-Olympics</topic>
      </item>
    
      <item ID="2009">
    
        <type>video</type>
        <thumb>thumbs/2009/02.jpg</thumb>
        <main>video/videos2009/2009 02.flv</main>
        <title>An Iar Chuairt Dheireannach</title>
        <description><![CDATA[<b>Anna McMullen</b> - An aghaidh]]></description>
        <school>rdsgoil Phort Rgh</school>
        <topic>Bu chir lan shaorsa a bhith aig na meadhanan leigeil le daoine am beachdan a chur an ceill</topic>
    
      </item>
    
      <item ID="2010">
    
        <type>video</type>
        <thumb>thumbs/2010/06.jpg</thumb>
        <main>video/videos2010/2010 06.flv</main>
        <title>An Iar Chuairt Dheireannach</title>
        <description><![CDATA[<b>Corrin Miller</b> - An aghaidh]]></description>
        <school>Acadamaidh Roghail Inbhir nis</school>
        <topic>Chan ann airson beatha-cosnaidh a bu chir do sgoiltean a bhith ag ullachadh sgoilearan idir</topic>
        
      </item>
    
    ETC
    Default.aspx.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.XPath;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
    
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("App_Data/XMLcat01.xml"));
            XmlNodeList nodes = doc.SelectNodes("mainTemplate/item");
    
            Repeater2.DataSourceID = null;
            Repeater2.DataSource = nodes;
            Repeater2.DataBind();
    
            String UserID = Request.QueryString["ID"];
    
            Response.Write("id detected = " + UserID);
    
            
        }
    }
    Default.aspx

    Code:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    
        <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    
        <script src="Scripts/jquery.lightbox-0.5.js" type="text/javascript"></script>
    
        <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
        <link href="css/thumbs.css" rel="stylesheet" type="text/css" />
    
        <script type="text/javascript">
            $(function () {
                $("#images  a").lightBox({ fixedNavigation: true });
    
            });
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:XmlDataSource ID="XmlDataSource1" runat="server">
    
            </asp:XmlDataSource>
    
            <asp:Repeater ID="Repeater2" runat="server" DataSourceID="XmlDataSource1">
    
            <ItemTemplate>
                    <div id="images">
                        <div style="width:573px">
                            <div id="data">
                                <ul id="pic">
                                  
                                    <li><%#((System.Xml.XmlNode)Container.DataItem).Attributes["ID"].Value %></li>
                                  
                                    <li><a href="<%#XPath("main")%>"/><img src="<%#XPath("thumb")%>"/></a></li>
                                    <li><%#XPath("title")%></li>
                                    <li><%#XPath("description")%></li>
                                </ul>
                            </div>                        
                        </div>
                    </div>
            </ItemTemplate>
            </asp:Repeater>
        </form>
    </body>
    </html>
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    An XPath expression (which is what SelectNodes() takes) that will find a specific node according to its "ID" attribute is
    Code:
    mainTemplate/item[@ID=123]
    (substitute 123 for the actual number, of course)

    Remember to make sure that UserID is actually a number - don't want to use just any string in there.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Western Isle of Scotland
    Posts
    258
    Rep Power
    17
    Originally Posted by requinix
    An XPath expression (which is what SelectNodes() takes) that will find a specific node according to its "ID" attribute is
    Code:
    mainTemplate/item[@ID=123]
    (substitute 123 for the actual number, of course)

    Remember to make sure that UserID is actually a number - don't want to use just any string in there.
    Thanks you

    sovled

    here's the script
    Code:
            String UserID = Request.QueryString["ID"];
    
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("App_Data/XMLcat01.xml"));
            XmlNodeList nodes = doc.SelectNodes("mainTemplate/item[@ID='"+ UserID +"']");

IMN logo majestic logo threadwatch logo seochat tools logo