C# LINQ到XML到数据表

C# LINQ到XML到数据表,c#,linq,xml-parsing,C#,Linq,Xml Parsing,我有以下xml fomat <feed> <entry> <s:product> <s:author> <s:name>amazone</s:name> <s:accountId>1111</s:accountId> </s:author> <s:title>xxx </s:title> <s:condit

我有以下xml fomat

<feed>
 <entry>
   <s:product>
   <s:author>
    <s:name>amazone</s:name>
    <s:accountId>1111</s:accountId>
   </s:author>
   <s:title>xxx
   </s:title>
   <s:condition>used
   </s:condition>
   <s:inventories>
    <s:inventory channel="online" availability="inStock">
     <s:price shipping="4.9" currency="EUR">14.95</s:price>
    </s:inventory>
   </s:inventories>
  </s:product>
 </entry>
 <entry>
  <s:product>
   <s:author>
    <s:name>ebay</s:name>
    <s:accountId>1221</s:accountId>
   </s:author>
   <s:title>yyy
   </s:title>
   <s:condition>new
   </s:condition>
   <s:inventories>
    <s:inventory channel="online" availability="inStock">
     <s:price shipping="4.90" currency="EUR">99.95</s:price>
    </s:inventory>
   </s:inventories>
  </s:product>
 </entry>  
</feed>
正如您所看到的,每个
都应该是一行,我不需要所有标记的值,只需要以下内容

name       condition       price      shipping      totalprice      availablity
amazone    used            14.95      4.95          19.90           inStock 
ebay       new             99.95      4.90          104.85          inStock 
column name       :value of <s:name>      
column condition  :value of <s:condition> 
column sprice     :value of <s:price>
column shipping   :value of shipping which is an attribute of <s:price>
column totalprice :value of <s:price> + its attribute shipping
column avilability:value of avialability an attribute of <s:inventory>
列名称:的值
列条件:的值
列精灵:的值
列shipping:shipping的值,它是的属性
列totalprice:值+其属性shipping
列可用性:可用性的值是的属性

如何使用LINQ实现这一点?

您的代码中有很多内容,大部分都不好。您只是没有正确使用LINQtoXML库

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
    );
}
  • 您的XML文档和您感兴趣的数据具有名称空间。您在文档中遗漏了名称空间声明,并且没有正确访问任何元素

    您需要为文档中使用的名称空间创建一个
    XNamespace
    变量集,并在尝试引用适当的元素时使用它

  • 您不能像这样将元素/属性(字符串)的值强制转换为double,在这种情况下,您必须解析它。然而,LINQtoXML提供了处理转换的显式强制转换。因此,可以对元素/属性本身强制转换为适当的类型,而不是它们包含的值。使用此选项,而不是操纵

    另一方面,无论何时处理货币,都不要使用
    double
    (或任何其他浮点类型),而是使用
    decimal

  • 如果不从数据表中创建数据行实例,则无法创建数据行实例。建议让您的查询首先关注数据。一旦有了数据,就为表创建新的数据行,然后再担心创建行和填充数据

话虽如此,下面是如何更恰当地使用它

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

我希望我能给你+10真的。