2010-07-24 2 views
0

Im пытается проанализировать следующий XML, используя LINQ-to-SQL, но визуальная студия, похоже, не хочет играть в мяч. Я попытался использовать XElement и XDocument и попытался выбрать из корневого узла или узла api_item напрямую, но, похоже, для меня это не похоже.Анализ XML с использованием LINQ to SQL

Может ли кто-нибудь посоветовать немного 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 

         }; 

Спасибо! (Heres фрагмент из 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 - поэтому я не могу загрузить его в свой редактор ... –

+0

К сожалению, здесь вы найдете: http://www.mediafire.com/?ht98c7rtqp2g572 – LDJ

+1

Это не LINQ to SQL - это LINQ to XML. –

ответ

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

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(); 
Смежные вопросы