locked
How to load different xml document with different URL RRS feed

  • Question

  • User643189141 posted

    Hi,

                XDocument doc = XDocument.Load("http://api./test1");
    XDocument doc = XDocument.Load("http://api./test2");

    How to load different xml document.

    Help

    Friday, August 2, 2013 11:01 AM

Answers

  • User643189141 posted

    In my case not load xmldocument if I used session. If I used querystring works.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 4, 2013 1:32 PM

All replies

  • User1508394307 posted
    XDocument doc = XDocument.Load("http://api./test1"); 
    XDocument doc2 = XDocument.Load("http://api./test2");
    Friday, August 2, 2013 11:25 AM
  • User643189141 posted

    and If I have Session["id"] ?

    XDocument doc = XDocument.Load("http://api./"+session[id]");
    Friday, August 2, 2013 1:11 PM
  • User1508394307 posted

    I'm curious what were previous 2352 posts about?

    XDocument doc = XDocument.Load("http://api./"+Session["id"].ToString());

    Friday, August 2, 2013 3:07 PM
  • User643189141 posted

    I'm curious what were previous 2352 posts about? Send me link what do you mean.

    The problem is because first load document and then shows.

    I have:

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["tId"] = "1000094981";
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Session["tId"] = "1000094985";
        }

    I must to click 2x button1 when shows Session["tId"] = "1000094981"

    Please help

    Friday, August 2, 2013 3:22 PM
  • User1508394307 posted

    Where do you show it?

    Friday, August 2, 2013 3:33 PM
  • User643189141 posted

    Hello,

    sorry for the delay answer.

    I have more links.

    When I clicked different links I must to load different data.
    So, If I have menu:
    England I want to show data(load) http://api./England
    France I want to show data(load) http://api./France

    Please help

    Sunday, August 4, 2013 8:49 AM
  • User643189141 posted

    I tested querystring and works. If I use Session["parameter1"] without querystring["parameter"] not works?

     Response.Redirect("http://localhost:53738/Default.aspx?parameter1=1000094981");

    Sunday, August 4, 2013 9:08 AM
  • User1508394307 posted

    Sorry, but it is not clear. What do you mean "not works"?

    Sunday, August 4, 2013 12:38 PM
  • User643189141 posted

    In my case not load xmldocument if I used session. If I used querystring works.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 4, 2013 1:32 PM
  • User1508394307 posted

    It is not clear because you didn't share the code. I think the problem might be in that fact that when redirecting the session cookie
    isn't being writen out. You can take a look at: http://weblogs.asp.net/bleroy/archive/2004/08/03/207486.aspx

    Monday, August 5, 2013 2:19 AM
  • User643189141 posted

    No,

    I have more xml document load link but for different parameters.

    api.test1.xml
    api.test2.xml
    api.test3.xml

    Now I want to load three different xml files with using Session.

    When I clicked Button1 I want to load test1.xml
    When I clicked Button2 I want to load test1.xml
    When I clicked Button3 I want to load test1.xml

    XDocument doc = XDocument.Load("http://api.test1.xml");
    XDocument doc = XDocument.Load("http://api.test1.xml");
    XDocument doc = XDocument.Load("http://api.test1.xml");

    public void ShowEvents()
        {
            if (Request.QueryString["EventId"] != null)
            {
                XDocument doc = XDocument.Load("http://api+Session["EventId"]);



        protected void Button1_Click(object sender, EventArgs e)
        {
          Session["EventId"]=test1
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
          Session["EventId"]=test2
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
          Session["EventId"]=test3
        }

    Please help


    Monday, August 5, 2013 4:12 AM
  • User1508394307 posted

    This has multiple errors

    public void ShowEvents()
        {
            if (Request.QueryString["EventId"] != null)
            {
                XDocument doc = XDocument.Load("http://api+Session["EventId"]);

    1. Concatenation of the strings is wrong. You should use quotes to indicate the beginning and ending of a string. So, must be "http://api" 
    2. Where is closing } fir if case and closing } for the void? 

    Monday, August 5, 2013 7:58 AM
  • User643189141 posted

    Check my code:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
    
    
    
    
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
    
        <div>
    
       
            <asp:DataGrid ID="grdData" AutoGenerateColumns="false" ShowHeader="false" 
                ShowFooter="false" GridLines="None" Width="100%" runat="server" DataKeyField="EventDate" 
                onitemdatabound="grdData_ItemDataBound" >
                <Columns>
                    <asp:TemplateColumn ItemStyle-BorderColor="Red" ItemStyle-BorderWidth="1"  >
                    <ItemTemplate>
                       <%-- <b> <%#Convert.ToDateTime(Eval("EventDate")).ToString("dddd - dd/mm/yyyy")%><br /></b>--%>
    
                        <br />
                        <asp:Label ID="lblLeague" style="color:Orange" text='<%#Eval("Topleague") %>' runat="server"></asp:Label>
                        <asp:Label ID="Label1" style="color:Orange" text='<%#Eval("EventDate") %>' runat="server"></asp:Label>
                        <br /><br />
                        
                        
                        <asp:DataGrid ID="grdDataInner2" Width="100%" ShowFooter="false" ShowHeader="false"
                                                    runat="server" AutoGenerateColumns="false" CellPadding="4"
                                                    GridLines="None">
                                                <Columns>
                                                        <asp:TemplateColumn >
                                                            <ItemTemplate>
    
                                                                <%--<asp:HiddenField ID="hdfEventDatePart" Value='<%#Convert.ToDateTime(Eval("EventDate")).ToString("dddd - dd/mm/yyyy")%>' runat="server" />--%>
                                                                <%--<asp:Label ID="lblTimePart" runat="server" Text='<%#Eval("Time")%>'></asp:Label>--%>
                                                                <b><%#Eval("Time")%></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                                <asp:Label ID="lblDescription" Text='<%#Eval("name") %>' runat="server"></asp:Label>
                                                                 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                                1- <%# Eval("oValue")%>
                                                                X- <%# Eval("Xcolvalue")%>
                                                                2- <%# Eval("2colvalue")%>
                                                                <div style="float:left; width:50px; text-align:center;">
    <%--                                                             <asp:LinkButton id="lnkoValue" OnCommand="selectodds_click" CommandArgument="oValue" class='button' runat="server" >
                                                                 <div class='button red' >
                                                                 <asp:Literal ID="ltOValue" runat="server" Text='<%# Eval("oValue")%>'></asp:Literal>
                                                                 </div>
                                                                 </asp:LinkButton>
                                                            </div>
    
                                                                <div style="float:left; width:50px; text-align:center;">
                                                                 <asp:LinkButton id="LinkButton1" OnCommand="selectodds_click" CommandArgument="Xcolvalue" class='button' runat="server" >
                                                                 <div class='button red' >
                                                                 <asp:Literal ID="ltXcolvalue" runat="server" Text='<%#Eval("Xcolvalue")%>'></asp:Literal>
                                                                 </div>
                                                                 </asp:LinkButton>
                                                            </div>
    
                                                                <div style="float:left; width:50px; text-align:center;">
                                                                 <asp:LinkButton id="LinkButton2" OnCommand="selectodds_click" CommandArgument="2colvalue" class='button' runat="server" >
                                                                 <div class='button red' >
                                                                 <asp:Literal ID="lt2colvalue" runat="server" Text='<%# Eval("2colvalue")%>'></asp:Literal>
                                                                 </div>
                                                                 </asp:LinkButton>--%>
                                                            </div>
    
     <%--                                                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                                <%# Eval("Xcol")%> - <%# Eval("Xcolvalue")%>
                                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                                2 - <%# Eval("2colvalue")%>--%>
                                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%--<%# Eval("Count")%>--%>
                                                            </ItemTemplate>
                                                        </asp:TemplateColumn>
                                                    </Columns>
                                                </asp:DataGrid>
                                    </ItemTemplate>
                                    </asp:TemplateColumn>
                                </Columns>
            </asp:DataGrid>
        
        </div>
                                </ContentTemplate>
    
    
            </asp:UpdatePanel>
        </form>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Globalization;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page
    {
    
           
    
        protected void Page_Load(object sender, EventArgs e)
        {    
    
            if (Session["EventId"] != null)
            {
                XDocument doc = XDocument.Load("http://api.unicdn.net/v1/feeds/sportsbook/betoffer/group/" + Session["EventId"].ToString() + ".xml?app_key=3da93299e6c670cd71895127ae445909&app_id=41d23c1d&type=2");
            
    
                    var distDates = (from c in doc.Descendants("events")
                                     where c.Element("type").Value == "ET_MATCH"
                                     select new
                                     {
                                         date = c.Element("start").Value.Split('T')[0].ToString(),
                                         Topleague = c.Element("group").Value,
                                     }).Distinct();
    
                    DataTable parentdt = new DataTable();
    
    
                    parentdt.Columns.Add("Topleague");
                    parentdt.Columns.Add("EventDate");
                    DataRow parentDr;
    
                    foreach (var dtdate in distDates)
                    {
                        parentDr = parentdt.NewRow();
                        parentDr["Topleague"] = dtdate.Topleague;
                        parentDr["EventDate"] = dtdate.date;
                        parentdt.Rows.Add(parentDr);
                    }
    
                    if (parentdt.Rows.Count > 0)
                    {
                        grdData.DataSource = parentdt;
                        grdData.DataBind();
                    }
                }
           // }
    
        }
    
        protected void grdData_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
    
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataGrid grdDataInner2 = (DataGrid)e.Item.FindControl("grdDataInner2");
                XDocument doc = XDocument.Load("http://api.unicdn.net/v1/feeds/sportsbook/betoffer/group/" + Session["EventId"].ToString() + ".xml?app_key=3da93299e6c670cd71895127ae445909&app_id=41d23c1d&type=2");
                var mainList = (from c in doc.Descendants("events")
                              //  from betoffers in doc.Descendants("betoffers")
                                where c.Element("start").Value.Split('T')[0].ToString() == DataBinder.Eval(e.Item.DataItem, "EventDate").ToString() && c.Element("type").Value == "ET_MATCH"
                                 select new
                                 {
                                     name = c.Element("name").Value,
                                     time = c.Element("start").Value.Split('T')[1].ToString().Replace('Z',' '),
                                     eventid= c.Element("id").Value,
                                  //   betoffers
                                 });
    
                DataTable dt = new DataTable();
                dt.Columns.Add("name");
                dt.Columns.Add("Time");
                dt.Columns.Add("EventId");
                dt.Columns.Add("oValue");
                dt.Columns.Add("Xcolvalue");
                dt.Columns.Add("2colvalue");
    
                foreach (var item in mainList)
                {
                    DataRow dr;
                    dr = dt.NewRow();
    
                    dr["name"] = item.name;
                    dr["time"] = item.time;
                    dr["EventId"] = item.eventid;
    
    
                    var Odds = (from events in doc.Descendants("betoffers")
                                where events.Element("eventId").Value == item.eventid && events.Element("criterion").Element("label").Value == "Full Time"
                                select events);
    
    
                    foreach (var itemOdds in Odds)
                    {
                        // here u need to loop like 
                        foreach (XElement element in itemOdds.Elements("outcomes"))
                            switch (element.Element("label").Value)
                            {
    
                                case "1":
                                    decimal myoValue = Convert.ToDecimal(element.Element("odds").Value);
                                    dr["oValue"] = myoValue.ToString("#,#").Substring(0,myoValue.ToString().Length-0); //String.Format("{0:f2}",element.Element("odds").Value.ToString()); // XmlConvert.ToDouble(element.Element("odds").Value.ToString()); //element.Element("odds").Value.ToString();
                                    break;
                                case "X":
                                    decimal myXcolvalue = Convert.ToDecimal(element.Element("odds").Value);
                                    dr["Xcolvalue"] = myXcolvalue.ToString("#,#").Substring(0, myXcolvalue.ToString().Length - 0);
                                    break;
                                case "2":
                                    decimal my2colvalue = Convert.ToDecimal(element.Element("odds").Value);
                                    dr["2colvalue"] = my2colvalue.ToString("#,#").Substring(0,my2colvalue.ToString().Length-0); //Convert.ToDecimal(element.Element("odds").Value.Length()-3,2); //element.Element("odds").Value.ToString();
                                    break;
                        }
                    }
     
                    dt.Rows.Add(dr);
                }
    
                grdDataInner2.DataSource = dt;
                grdDataInner2.DataBind();
            }
        }
    
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["EventId"] = "1000094981";
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Session["EventId"] = "1000094985";
        }

    The problem is because when I clicked Button1 or Button2, I got records 0, then I again clicked on the Button1 or Button2 and then shows result.

    Please help,




    Monday, August 5, 2013 1:29 PM
  • User1508394307 posted

    Just for my understanding, what is the reason of using session and not querystring in 

     protected void Button1_Click(object sender, EventArgs e)
     {
         Session["EventId"] = "1000094981";
     }
     protected void Button2_Click(object sender, EventArgs e)
     {
         Session["EventId"] = "1000094985";
     }

    ?

    Monday, August 5, 2013 1:36 PM
  • User643189141 posted

    Because querystring refreshed my website.

    If I used querystring is it possible without refresh?

    Monday, August 5, 2013 1:41 PM
  • User1508394307 posted

    Let's see what you currently have  

    1) you have a page that at the first run does not show anything except 2 buttons.
    2) clicking on one of the buttons do not do anything but show again an empty page
    3) when refreshing or navigating page again you get remote xml and show it in the grid
    4) going somewhere and returing back shows the same grid

    Now, if I understand it correct you want  

    1) a page that at the first run does not show anything except 2 buttons.
    2) clicking on one of the buttons you get remote xml and show it in the grid

    If true, you'll need to redesign your code as follows

    1) all the code from the Page_Load() should be moved to a new void, e.g. BindGrid(). 
    2) references to Session should be removed "if (Session["EventId"] != null)" and "+ Session["EventId"].ToString()"
    3) instead you can use a new string parameter of BindGrid() - e.g. BindGrid(string EventId)
    4) both click events Button1_Click and Button2_Click should call BindGrid with corresponding parameter, e.g. BindGrid("1000094981")

    Tuesday, August 6, 2013 3:35 AM
  • User643189141 posted

    Thanks works. I have problem with
    protected void grdData_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

    How to call string EventId in grdData_ItemDataBound

    My code:

    void BindGrid(string EventId)
        {
          //  if (Session[]Request.QueryString["EventId"] != null)
         //   {
            XDocument doc = XDocument.Load("http://api.unicdn.net/v1/feeds/sportsbook/betoffer/group/" + EventId + ".xml?app_key=3da93299e6c670cd71895127ae445909&app_id=41d23c1d&type=2");
    
                // XDocument doc = XDocument.Load("http://api.unicdn.net/v1/feeds/sportsbook/betoffer/group/" + Session["EventId"].ToString() + ".xml?app_key=3da93299e6c670cd71895127ae445909&app_id=41d23c1d&type=2");
    
    
                var distDates = (from c in doc.Descendants("events")
                                 where c.Element("type").Value == "ET_MATCH"
                                 select new
                                 {
                                     date = c.Element("start").Value.Split('T')[0].ToString(),
                                     Topleague = c.Element("group").Value,
                                 }).Distinct();
    
                DataTable parentdt = new DataTable();
    
    
                parentdt.Columns.Add("Topleague");
                parentdt.Columns.Add("EventDate");
                DataRow parentDr;
    
                foreach (var dtdate in distDates)
                {
                    parentDr = parentdt.NewRow();
                    parentDr["Topleague"] = dtdate.Topleague;
                    parentDr["EventDate"] = dtdate.date;
                    parentdt.Rows.Add(parentDr);
                }
    
                if (parentdt.Rows.Count > 0)
                {
                    grdData.DataSource = parentdt;
                    grdData.DataBind();
                }
           // }
            // }
        }
    
    protected void grdData_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
    
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataGrid grdDataInner2 = (DataGrid)e.Item.FindControl("grdDataInner2");
                XDocument doc = XDocument.Load("http://api.unicdn.net/v1/feeds/sportsbook/betoffer/group/" + "1000094981" + ".xml?app_key=3da93299e6c670cd71895127ae445909&app_id=41d23c1d&type=2");
                var mainList = (from c in doc.Descendants("events")
                              //  from betoffers in doc.Descendants("betoffers")
                                where c.Element("start").Value.Split('T')[0].ToString() == DataBinder.Eval(e.Item.DataItem, "EventDate").ToString() && c.Element("type").Value == "ET_MATCH"
                                 select new
                                 {
                                     name = c.Element("name").Value,
                                     time = c.Element("start").Value.Split('T')[1].ToString().Replace('Z',' '),
                                     eventid= c.Element("id").Value,
                                  //   betoffers
                                 });
    
                DataTable dt = new DataTable();
                dt.Columns.Add("name");
                dt.Columns.Add("Time");
                dt.Columns.Add("EventId");
                dt.Columns.Add("oValue");
                dt.Columns.Add("Xcolvalue");
                dt.Columns.Add("2colvalue");
    
                foreach (var item in mainList)
                {
                    DataRow dr;
                    dr = dt.NewRow();
    
                    dr["name"] = item.name;
                    dr["time"] = item.time;
                    dr["EventId"] = item.eventid;
    
    
                    var Odds = (from events in doc.Descendants("betoffers")
                                where events.Element("eventId").Value == item.eventid && events.Element("criterion").Element("label").Value == "Full Time"
                                select events);
    
    
                    foreach (var itemOdds in Odds)
                    {
                        // here u need to loop like
                        foreach (XElement element in itemOdds.Elements("outcomes"))
                            switch (element.Element("label").Value)
                            {
    
                                case "1":
                                    decimal myoValue = Convert.ToDecimal(element.Element("odds").Value);
                                    dr["oValue"] = myoValue.ToString("#,#").Substring(0,myoValue.ToString().Length-0); //String.Format("{0:f2}",element.Element("odds").Value.ToString()); // XmlConvert.ToDouble(element.Element("odds").Value.ToString()); //element.Element("odds").Value.ToString();
                                    break;
                                case "X":
                                    decimal myXcolvalue = Convert.ToDecimal(element.Element("odds").Value);
                                    dr["Xcolvalue"] = myXcolvalue.ToString("#,#").Substring(0, myXcolvalue.ToString().Length - 0);
                                    break;
                                case "2":
                                    decimal my2colvalue = Convert.ToDecimal(element.Element("odds").Value);
                                    dr["2colvalue"] = my2colvalue.ToString("#,#").Substring(0,my2colvalue.ToString().Length-0); //Convert.ToDecimal(element.Element("odds").Value.Length()-3,2); //element.Element("odds").Value.ToString();
                                    break;
                        }
                    }
     
                    dt.Rows.Add(dr);
                }
    
                grdDataInner2.DataSource = dt;
                grdDataInner2.DataBind();
            }

    Tuesday, August 6, 2013 4:45 AM
  • User1508394307 posted

    If you want to access EventId that was assigned in BindGrid then the easiest way is probably to create a class level variable, e.g. static string _eventID; that could be assign either in BindGrid or on a button click.

    Wednesday, August 7, 2013 3:55 AM