【问题标题】:Reading a complex XML file using linq [duplicate]使用 linq 读取复杂的 XML 文件 [重复]
【发布时间】:2013-05-19 03:11:07
【问题描述】:

我正在尝试使用 LinQ 读取复杂的 XML 文件。

XML 文件有很多层次,如何在一个 ILIST 中获取所有值。项目中还有更多标签。enter code here

XML 具有以下语法:

<root>
  <items>
    <index_0>
      <product_id>19</product_id>
      <menu_rank>2</menu_rank>
      <menu_country>Guatemala</menu_country>
      <menu_country_code>502</menu_country_code>
      <menu_country_abrv>GT</menu_country_abrv>
      <menu_carrier>TIGO</menu_carrier>
      <menu_value>7.0</menu_value>
    </index_0>
    <index_1>
      <product_id>20</product_id>
      <menu_rank>2</menu_rank>
      <menu_country>Guatemala</menu_country>
      <menu_country_code>502</menu_country_code>
      <menu_country_abrv>GT</menu_country_abrv>
      <menu_carrier>TIGO</menu_carrier>
      <menu_value>10.0</menu_value>
    </index_1>
    <index_2>
      <product_id>21</product_id>
      <menu_rank>2</menu_rank>
      <menu_country>Guatemala</menu_country>
      <menu_country_code>502</menu_country_code>
      <menu_country_abrv>GT</menu_country_abrv>
      <menu_carrier>TIGO</menu_carrier>
      <menu_value>14.0</menu_value>
    </index_2>
  </items>
</root>

我尝试过这种方法但没有成功:

var chartrate = from a in xmlDoc.Descendants("items")
                select new 
                {
                  sub_id = a.Element("product_id").Value,
                  product = a.Element("menu_rank").Value,
                  description = a.Element("menu_country").Value
                } ;

有什么建议吗?

【问题讨论】:

  • 试图在元素名称中嵌入数据(index_0、index_1、...)会给您带来麻烦。如果可以,请将其更改为 &lt;item index="0"&gt;。

标签: c# xml linq


【解决方案1】:

我看到的最大问题是product_id、menu_rank 和menu_country 都不是items 的元素

解决此问题的最佳方法可能是实际更改 XML 架构(如果可以),使其实际上表示 items 元素的列表。

<root>
 <items>
   <product_id>19</product_id>
   <menu_rank>2</menu_rank>
   <menu_country>Guatemala</menu_country>
   <menu_country_code>502</menu_country_code>
   <menu_country_abrv>GT</menu_country_abrv>
   <menu_carrier>TIGO</menu_carrier>
   <menu_value>7.0</menu_value>
 </items>
 <items>
   <product_id>20</product_id>
   <menu_rank>2</menu_rank>
   <menu_country>Guatemala</menu_country>
   <menu_country_code>502</menu_country_code>
   <menu_country_abrv>GT</menu_country_abrv>
   <menu_carrier>TIGO</menu_carrier>
   <menu_value>10.0</menu_value>
 </items>
 <items>
   <product_id>21</product_id>
   <menu_rank>2</menu_rank>
   <menu_country>Guatemala</menu_country>
   <menu_country_code>502</menu_country_code>
   <menu_country_abrv>GT</menu_country_abrv>
   <menu_carrier>TIGO</menu_carrier>
   <menu_value>14.0</menu_value>
 </items>
</root>

完成此操作后,您可以在 xml 和 c# 中将 items 重命名为 item

【讨论】:

    【解决方案2】:

    这应该可行:

    using System.Xml.XPath;
    ...
    
    var chartrate = from a in xmlDoc.XPathSelectElements ("/root/items/*")
    
                         select new 
                         {
                             sub_id = a.Element("product_id").Value,
                             product = a.Element("menu_rank").Value,
                             description = a.Element("menu_country").Value
    
                         };
    

    XPath 查询选择所有元素,无论它在/root/items 元素下的名称是什么(参见*)

    【讨论】:

      【解决方案3】:

      这应该可以工作

      var result = X.Element("root")
                      .Element("items")
                      .Elements().Where(E => E.Name.ToString().Contains("index"))
                      .Select(E => new { Id = E.Element("product_id").Value, Country = E.Element("menu_country").Value });
      

      你应该看看 Sam 我的回答,如果可能的话尝试修改你的 XML。

      【讨论】:

        【解决方案4】:

        您只需要另一个级别的间接来检索您的 Index_N 元素:

        XElement xmlDoc = XElement.Parse(xml);
        var chartrate = 
            from a in xmlDoc.Descendants("items").Elements()
            select new 
            {
                sub_id = a.Element("product_id").Value,
                product = a.Element("menu_rank").Value,
                description = a.Element("menu_country").Value
            };
        
        chartrate.Dump();
        

        在LinqPad 中,这会产生:

        sub_id product description 
        19 2 Guatemala 
        20 2 Guatemala 
        21 2 Guatemala 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-21
          • 1970-01-01
          相关资源
          最近更新 更多