2010-07-24 100 views
0

我試圖使用LINQ to SQL來解析以下XML,但visual studio似乎不想玩球。我嘗試過使用XElement和XDocument,並嘗試直接從根節點或api_item節點進行選擇,但似乎並未發生。使用LINQ to SQL解析XML

任何人都可以建議一點LINQ到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 

         }; 

謝謝! (繼承人的XML的一個片段來從後面API)

  <?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> 
+0

你有一個鏈接到完整的XML?這段代碼是無效的XML - 所以我不能加載到我的編輯器... – 2010-07-24 17:36:11

+0

對不起,你在這裏: http://www.mediafire.com/?ht98c7rtqp2g572 – LDJ 2010-07-24 17:39:56

+1

這不是LINQ to SQL - 它是LINQ to XML。 – 2010-07-24 17:43:52

回答

2

想通了:

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

看起來它不會工作,除非我賦予它之前投的元素值。添加鑄件似乎有訣竅。多麼奇怪!

0

您可以使用這種方法進行的快速教程退房LINQ to XML - 5 Minute Overview

+0

這就是我從上面的代碼示例,但出於某種原因似乎不能在上述XML – LDJ 2010-07-24 20:38:05

1

試試這個:

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