【问题标题】:Parse XML using LINQ to SQL使用 LINQ to SQL 解析 XML
【发布时间】:2010-07-24 17:22:34
【问题描述】:

我正在尝试使用 LINQ-to-SQL 解析以下 XML,但 Visual Studio 似乎不想玩。我尝试过使用 XElement 和 XDocument 并尝试直接从根节点或 api_item 节点中进行选择,但对我来说似乎没有发生。

谁能提供一些 LINQ-to-XML 的建议?

我尝试了以下(以及它的许多变体!)但无济于事。 (注意:e.Result 包含 XML 字符串)

var deals = from el in XElement.Parse(e.Result).Elements("api_response").Elements("deals").Elements("api_item")
                        select new
                        {
                            title = el.Element("title").Value,
                            description = el.Element("description").Value

                        };

谢谢! (这里是从 API 返回的 XML 的 sn-p)

         <?xml version="1.0" ?> 
    - <api_response>
    - <deals>
    - <api_item>
      <title>Palit GeForce GTX460 768MB: £143.56 at CCLOnline</title> 
      <deal_link>http://www.hotukdeals.com/deals/palit-geforce-gtx460-768mb-143-56-a/725851</deal_link> 
      <mobile_deal_link>http://m.hotukdeals.com/deals/palit-geforce-gtx460-768mb-143-56-a/725851</mobile_deal_link> 
      <deal_image>http://www.hotukdeals.com/images/threads/725851_1.jpg</deal_image> 
      <description>Palit GeForce GTX460 768MB - NE5TX460FHD79 (Free delivery or collection) Graphics card require: ..PCI Express or PCI Express 2.0-compliant motherboard with one dual-width x16 graphics slot ..Two 6-pin PCI Express supplementary power connectors ..Minimum 450W or greater system power supply (with a minimum 12V current rating of 24A) ..Supported Operating Systems: Windows 7, Windows Vista, Windows XP</description> 
      <submit_time>19 hours, 29 minutes ago</submit_time> 
      <hot_time>8 minutes ago</hot_time> 
      <poster_name>gads</poster_name> 
      <temperature>102.629997253</temperature> 
      <price>143.56</price> 
      <timestamp>1279921047</timestamp> 
      <expired>false</expired> 
    - <forum>
      <name>Deals</name> 
      <url_name>deals</url_name> 
      </forum>
    - <category>
      <name>Computers</name> 
      <url_name>computers</url_name> 
      </category>
    - <merchant>
      <name>CCLOnline</name> 
      <url_name>cclonline</url_name> 
      </merchant>
      <tags /> 
      </api_item>
    - <api_item>
      <title>Empire Total War (PC) £10 instore @ HMV!</title> 
      <deal_link>http://www.hotukdeals.com/deals/empire-total-war-pc-10-instore-hmv/725848</deal_link> 
      <mobile_deal_link>http://m.hotukdeals.com/deals/empire-total-war-pc-10-instore-hmv/725848</mobile_deal_link> 
      <deal_image>http://www.hotukdeals.com/images/threads/725848_1.jpg</deal_image> 
      <description>Not the latest game in the series, but still a cracking game, and about a fiver cheaper than the best online price by the looks of things.</description> 
      <submit_time>19 hours, 34 minutes ago</submit_time> 
      <hot_time>8 minutes ago</hot_time> 
      <poster_name>Crazy Jamie</poster_name> 
      <temperature>98.4000015259</temperature> 
      <price>10</price> 
      <timestamp>1279920760</timestamp> 
      <expired>false</expired> 
    - <forum>
      <name>Deals</name> 
      <url_name>deals</url_name> 
      </forum>
    - <category>
      <name>Entertainment</name> 
      <url_name>entertainment</url_name> 
      </category>
    - <merchant>
      <name>HMV</name> 
      <url_name>hmv</url_name> 
      </merchant>
    - <tags>
    - <api_item>
      <name>empire total war</name> 
      </api_item>
      </tags>
      </api_item>
    - <api_item>
      <title>Samsung SH-B083L 8x Bluray ROM + DVD-RW - £46.72 Delivered or LESS @ Overclockers</title> 
      <deal_link>http://www.hotukdeals.com/deals/samsung-sh-b083l-8x-bluray-rom-dvd-/725984</deal_link> 
      <mobile_deal_link>http://m.hotukdeals.com/deals/samsung-sh-b083l-8x-bluray-rom-dvd-/725984</mobile_deal_link> 
      <deal_image>http://www.hotukdeals.com/images/threads/725984_1.jpg</deal_image> 
      <description>If you get free delivery from Overclockers by being a long-time forums member then its just £43.46 delivered and its the same price if you buy from their shop in Stoke. Samsung's SH-B083L can play Blu-ray discs and record to CDs and DVDs. Plus, it has fast read speeds, which go all the way up to 8x. In addition to 1080p HD picture support , the SHB083L is available with a huge capacity of up to 25 GB for a single-layer disc, and 50 GB for a dual-layer disc. You can also breathe new life into your current movie collection with the latest upscaling technology this gives your standard definition DVDs a dramatically much improved high resolution look. Write Speed - DVD-R: 16x - DVD-R DL: 8x - DVD+R: 16x - DVD+R DL: 8x - DVD+RW: 6x - DVD-RAM: 12x - CD-R: 48x - CD-RW: 4x Read Speed - BD-R: 8x - BD-R DL: 4x - BD-RE DL: 4x - DVD-RAM: 12x - DVD-ROM: 16x - DVD-R: 16x - DVD+R: 16x - DVD+RW: 12x - CD-R: 48x - CD-RW: 40x - CD-ROM: 48x</description> 
      <submit_time>6 hours, 38 minutes ago</submit_time> 
      <hot_time>9 minutes ago</hot_time> 
      <poster_name>hajj_3</poster_name> 
      <temperature>96.4800033569</temperature> 
      <price>46.72</price> 
      <timestamp>1279967326</timestamp> 
      <expired>false</expired> 
    - <forum>
      <name>Deals</name> 
      <url_name>deals</url_name> 
      </forum>
    - <category>
      <name>Computers</name> 
      <url_name>computers</url_name> 
      </category>
    - <merchant>
      <name>Overclockers</name> 
      <url_name>overclockers</url_name> 
      </merchant>
      <tags /> 
      </api_item>
  <?xml version="1.0" ?> 
- <api_response>
- <deals>
- <api_item>
  <title>Palit GeForce GTX460 768MB: £143.56 at CCLOnline</title> 
  <deal_link>http://www.hotukdeals.com/deals/palit-geforce-gtx460-768mb-143-56-a/725851</deal_link> 
  <mobile_deal_link>http://m.hotukdeals.com/deals/palit-geforce-gtx460-768mb-143-56-a/725851</mobile_deal_link> 
  <deal_image>http://www.hotukdeals.com/images/threads/725851_1.jpg</deal_image> 
  <description>Palit GeForce GTX460 768MB - NE5TX460FHD79 (Free delivery or collection) Graphics card require: ..PCI Express or PCI Express 2.0-compliant motherboard with one dual-width x16 graphics slot ..Two 6-pin PCI Express supplementary power connectors ..Minimum 450W or greater system power supply (with a minimum 12V current rating of 24A) ..Supported Operating Systems: Windows 7, Windows Vista, Windows XP</description> 
  <submit_time>19 hours, 29 minutes ago</submit_time> 
  <hot_time>8 minutes ago</hot_time> 
  <poster_name>gads</poster_name> 
  <temperature>102.629997253</temperature> 
  <price>143.56</price> 
  <timestamp>1279921047</timestamp> 
  <expired>false</expired> 
- <forum>
  <name>Deals</name> 
  <url_name>deals</url_name> 
  </forum>
- <category>
  <name>Computers</name> 
  <url_name>computers</url_name> 
  </category>
- <merchant>
  <name>CCLOnline</name> 
  <url_name>cclonline</url_name> 
  </merchant>
  <tags /> 
  </api_item>
- <api_item>
  <title>Empire Total War (PC) £10 instore @ HMV!</title> 
  <deal_link>http://www.hotukdeals.com/deals/empire-total-war-pc-10-instore-hmv/725848</deal_link> 
  <mobile_deal_link>http://m.hotukdeals.com/deals/empire-total-war-pc-10-instore-hmv/725848</mobile_deal_link> 
  <deal_image>http://www.hotukdeals.com/images/threads/725848_1.jpg</deal_image> 
  <description>Not the latest game in the series, but still a cracking game, and about a fiver cheaper than the best online price by the looks of things.</description> 
  <submit_time>19 hours, 34 minutes ago</submit_time> 
  <hot_time>8 minutes ago</hot_time> 
  <poster_name>Crazy Jamie</poster_name> 
  <temperature>98.4000015259</temperature> 
  <price>10</price> 
  <timestamp>1279920760</timestamp> 
  <expired>false</expired> 
- <forum>
  <name>Deals</name> 
  <url_name>deals</url_name> 
  </forum>
- <category>
  <name>Entertainment</name> 
  <url_name>entertainment</url_name> 
  </category>
- <merchant>
  <name>HMV</name> 
  <url_name>hmv</url_name> 
  </merchant>
- <tags>
- <api_item>
  <name>empire total war</name> 
  </api_item>
  </tags>
  </api_item>
- <api_item>
  <title>Samsung SH-B083L 8x Bluray ROM + DVD-RW - £46.72 Delivered or LESS @ Overclockers</title> 
  <deal_link>http://www.hotukdeals.com/deals/samsung-sh-b083l-8x-bluray-rom-dvd-/725984</deal_link> 
  <mobile_deal_link>http://m.hotukdeals.com/deals/samsung-sh-b083l-8x-bluray-rom-dvd-/725984</mobile_deal_link> 
  <deal_image>http://www.hotukdeals.com/images/threads/725984_1.jpg</deal_image> 
  <description>If you get free delivery from Overclockers by being a long-time forums member then its just £43.46 delivered and its the same price if you buy from their shop in Stoke. Samsung's SH-B083L can play Blu-ray discs and record to CDs and DVDs. Plus, it has fast read speeds, which go all the way up to 8x. In addition to 1080p HD picture support , the SHB083L is available with a huge capacity of up to 25 GB for a single-layer disc, and 50 GB for a dual-layer disc. You can also breathe new life into your current movie collection with the latest upscaling technology this gives your standard definition DVDs a dramatically much improved high resolution look. Write Speed - DVD-R: 16x - DVD-R DL: 8x - DVD+R: 16x - DVD+R DL: 8x - DVD+RW: 6x - DVD-RAM: 12x - CD-R: 48x - CD-RW: 4x Read Speed - BD-R: 8x - BD-R DL: 4x - BD-RE DL: 4x - DVD-RAM: 12x - DVD-ROM: 16x - DVD-R: 16x - DVD+R: 16x - DVD+RW: 12x - CD-R: 48x - CD-RW: 40x - CD-ROM: 48x</description> 
  <submit_time>6 hours, 38 minutes ago</submit_time> 
  <hot_time>9 minutes ago</hot_time> 
  <poster_name>hajj_3</poster_name> 
  <temperature>96.4800033569</temperature> 
  <price>46.72</price> 
  <timestamp>1279967326</timestamp> 
  <expired>false</expired> 
- <forum>
  <name>Deals</name> 
  <url_name>deals</url_name> 
  </forum>
- <category>
  <name>Computers</name> 
  <url_name>computers</url_name> 
  </category>
- <merchant>
  <name>Overclockers</name> 
  <url_name>overclockers</url_name> 
  </merchant>
  <tags /> 
  </api_item>
</deals>
  <total_results>1000</total_results> 
  </api_response>

【问题讨论】:

  • 您有完整 XML 的链接吗?那个 sn-p 不是有效的 XML - 所以我无法将它加载到我的编辑器中......
  • 对不起,给你:mediafire.com/?ht98c7rtqp2g572
  • 这不是 LINQ to SQL - 这是 LINQ to XML。
  • 哈哈。找出错字!我什至将它标记为 Linq-to-xml。大声笑

标签: linq-to-xml


【解决方案1】:

想通了:

var deals = from el in XElement.Parse(e.Result).Elements("deals").Elements("api_item")
            select new
            {
               title = (string)el.Element("title").Value,
               description = (string)el.Element("description").Value
            };

除非我在分配元素值之前强制转换它,否则它似乎不起作用。添加铸件似乎可以解决问题。多么奇怪!

【讨论】:

    【解决方案2】:

    试试这个:

    XDocument feed = XDocument.Load("the link for the xml file");
    feed.Save("type the link here");            
    
    DataClasses1DataContext db = new DataClasses1DataContext();
    
    
    var feedpost = from c in feed.Descendants("api_item")
                   select new Table_1
                   {
                       Title = c.Element("title").Value,
                       description = c.Element("description").Value
    
                   };
    db.Table_1s.InsertAllOnSubmit(feedpost);
    db.SubmitChanges();
    

    【讨论】:

      【解决方案3】:

      您可以查看LINQ to XML - 5 Minute Overview 以获取有关使用该方法的快速教程。

      【讨论】:

      • 那是我从上面获取代码示例的地方,但由于某种原因似乎不适用于上面的 XML
      猜你喜欢
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多