Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have loaded XML data into a variable .now how should i fetch the information and laod it into table using LINQ...?

What I have tried:

C#
namespace Bettorlogic.Optacore.HistoricalData.Console
{

    class gsmrs
    {
        public string sport { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            RunAsync().Wait();
        }

        static async Task RunAsync()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://api.core.optasports.com/");
                  
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
                    // New code:
                    HttpResponseMessage response = await client.GetAsync("soccer/get_seasons?authorized=yes&username=betlogic&authkey=8277e0910d750195b448797616e091ad");
                    if (response.IsSuccessStatusCode)
                    {
                        var gsmr =  response.Content.ReadAsStringAsync().Result;
                        //Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}<pre>
Posted
Updated 8-Mar-16 20:03pm
v2

1 solution

You could make use of the LINQ to XML library
...anywhere, there are tons of examples here on codeproject that should give you an insight.

C#
XDocument doc = GetDocument();
// assuming this is the correct namespace
XNamespace s = "http://www.google.com/shopping/api/schemas/2010";
var query =
    from product in doc.Root.Elements("entry").Elements(s + "product")
    // declare some variables to make the query cleaner
    let inventory = product.Element(s + "inventories").Element(s + "inventory")
    let price = (decimal)inventory.Element(s + "price")
    let shipping = (decimal)inventory.Element(s + "price").Attribute("shipping")
    select new
    {
        Name = (string)product.Element(s + "author").Element(s + "name"),
        Condition = (string)product.Element(s + "condition"),
        Price = price,
        Shipping = shipping,
        TotalPrice = price + shipping,
        Availability = (string)inventory.Attribute("availability"),
    };

var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Condition", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
dt.Columns.Add("Shipping", typeof(decimal));
dt.Columns.Add("TotalPrice", typeof(decimal));
dt.Columns.Add("Availability", typeof(string));
foreach (var product in query)
{
    dt.Rows.Add(
        product.Name,
        product.Condition,
        product.Price,
        product.Shipping,
        product.TotalPrice,
        product.Availability
    );
}
 
Share this answer
 
Comments
PrashanthRaj16 9-Mar-16 2:46am    
thank you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900