c# - LINQ to XML 到数据表

标签 c# linq xml-parsing

我有以下 xml 格式

<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 

正如你所看到的每个 <entry>应该是一行,我不需要所有标签的值,只需要以下

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>

如何使用 LINQ 实现此目的?

最佳答案

您的代码中发生了很多事情,其中​​大部分都不好。您只是没有正确使用 LINQ to XML 库。

  • 您的 XML 文档和您感兴趣的数据都有命名空间。您在文档中遗漏了 namespace 声明,并且无法正确访问任何元素。

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

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

    顺便说一句,每当您处理货币时,切勿使用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
    );
}

关于c# - LINQ to XML 到数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15192000/

相关文章:

c# - 检测发生 rightTapped 事件的 ListViewitem

c# - 用于 C# 的 WSDL 生成器

c# - LINQ LEFT JOIN where 子句不起作用

linux - Windows 与 Linux std::getline 字符串

c# - 尝试在现有工作簿中选择命名的 Excel 2007 表并插入行

c# - 从 Android App 中的文件读取时出现 IsolatedStorage 异常

C# 获取集合中最流行的 2 项组合

c# - 使用ListView控件显示数据但加入一个引用表

c# - 用于选择特定节点下的节点(中间有零个或多个节点)的 XPath

python - iterparse 解析一个字段失败,而其他类似的没问题