如何使用linq将xml数据加载到表中

如何使用linq将xml数据加载到表中

问题描述:

我已将XML数据加载到变量中。现在我应该如何获取信息并使用LINQ将其存入表中?...



什么我试过了:



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:

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>

{1 } \t {2},product.Name,product.Price,product.Category);
}
}
}
catch (Exception ex)
{

throw ex;
}
}
}
}< pre>
{1}\t{2}", product.Name, product.Price, product.Category); } } } catch (Exception ex) { throw ex; } } } }<pre>


您可以使用LINQ to XML库

...在任何地方,codeproject上都有大量的例子可以给你一个见解。



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.

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
    );
}