2013-03-12 132 views
0

我使用他們的odata feed從eBay提取信息。我正在嘗試使用linq來獲取我們之後的具體信息。使用linq我可以下載到保存我們想要的信息的特定元素。我不能做的是弄清楚如何查詢元素數據以獲取我想要的特定子元素。我可以解析它,但真的想學習linq。我使用vb.net作爲語言。之後我用下面要獲得我的元素:查詢ebay odata與linq

Sub Main 
dim ns = "http://ebayodata.cloudapp.net/" 
dim url as string = "http://ebayodata.cloudapp.net/Items?search=1756-L65" 
    Using reader As XmlReader = XmlReader.Create(url) 
    While reader.Read() 
     If reader.NodeType = XmlNodeType.Element AndAlso reader.Name = "entry" Then 
      GetChildContentElements(reader) 
     End If 

    End While 
End Using 
End Sub 


    Private Sub GetChildContentElements(reader As XmlReader) 
' move to first child 
While reader.Read() 
    If reader.NodeType = XmlNodeType.Element AndAlso reader.Name = "m:properties" Then 
     Exit While 
    End If 
End While 
Dim bookXml As XElement = DirectCast(XNode.ReadFrom(reader), XElement) 
Console.WriteLine(bookXml) 

    End Sub 

一本返回元素的樣子:

<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"> 
    <d:Id>160917851201</d:Id> 
    <d:UserId>baltisales</d:UserId> 
    <d:Title>Allen Bradley 1756-L65 /B ControlLogix Processor 32MB Memory *60 DAYS WARRANTY!*</d:Title> 
    <d:Subtitle m:null="true"></d:Subtitle> 
    <d:SellingState>Active</d:SellingState> 
    <d:TimeLeft>P24DT2H25M33S</d:TimeLeft> 
    <d:Currency>USD</d:Currency> 
    <d:CurrentPrice m:type="Edm.Double">6446.14</d:CurrentPrice> 
    <d:MinimumToBid m:type="Edm.Double">6446.14</d:MinimumToBid> 
    <d:BidCount m:type="Edm.Int32">0</d:BidCount> 
    <d:Description m:null="true"></d:Description> 
    <d:QuantitySold m:type="Edm.Int32">0</d:QuantitySold> 
    <d:AutoPay m:type="Edm.Boolean">false</d:AutoPay> 
    <d:CharityId m:null="true"></d:CharityId> 
    <d:Country>US</d:Country> 
    <d:Compatibility m:null="true"></d:Compatibility> 
    <d:GalleryUrl>http://thumbs2.ebaystatic.com/m/m3Y01PfuyFhctnJiEet95Gw/140.jpg</d:GalleryUrl> 
    <d:GlobalId>EBAY-US</d:GlobalId> 
    <d:PostalCode>21209</d:PostalCode> 
    <d:ReturnsAccepted m:type="Edm.Boolean">true</d:ReturnsAccepted> 
    <d:PrimaryCategoryId>97184</d:PrimaryCategoryId> 
    <d:SecondaryCategoryId m:null="true"></d:SecondaryCategoryId> 
    <d:ViewItemUrl>http://www.ebay.com/itm/Allen-Bradley-1756-L65-B-ControlLogix-Processor-32MB-Memory-60-DAYS-WARRANTY-/160917851201?pt=BI_Control_Systems_PLCs</d:ViewItemUrl> 
    <d:PaymentMethods>PayPal ,VisaMC ,AmEx</d:PaymentMethods> 
    <d:Condition m:type="eBay.Model.Entities.Condition"> 
    <d:Id m:type="Edm.Int32">3000</d:Id> 
    <d:Name>Used</d:Name> 
    </d:Condition> 
    <d:ListingInfo m:type="eBay.Model.Entities.ListingInfo"> 
    <d:BestOfferEnabled m:type="Edm.Boolean">true</d:BestOfferEnabled> 
    <d:BuyItNowAvailable m:type="Edm.Boolean">false</d:BuyItNowAvailable> 
    <d:BuyItNowPrice m:type="Edm.Double" m:null="true"></d:BuyItNowPrice> 
    <d:ConvertedBuyItNowPrice m:type="Edm.Double" m:null="true"> </d:ConvertedBuyItNowPrice> 
    <d:Gift m:type="Edm.Boolean">false</d:Gift> 
    <d:ListingType>StoreInventory</d:ListingType> 
    <d:StartTime m:type="Edm.DateTime">2012-11-06T23:08:18Z</d:StartTime> 
    <d:EndTime m:type="Edm.DateTime">2013-04-05T23:13:18Z</d:EndTime> 
    </d:ListingInfo> 
    <d:Distance m:type="eBay.Model.Entities.Distance" m:null="true"></d:Distance> 
    <d:ShippingInformation m:type="eBay.Model.Entities.ShippingInformation"> 
    <d:Delimiter m:null="true"></d:Delimiter> 
    <d:ExpeditedShipping m:type="Edm.Boolean">true</d:ExpeditedShipping> 
    <d:HandlingTime m:type="Edm.Int32">1</d:HandlingTime> 
    <d:OneDayShippingAvailable m:type="Edm.Boolean">true</d:OneDayShippingAvailable> 
    <d:ShippingServiceCost m:type="Edm.Double">0</d:ShippingServiceCost> 
    <d:ShippingType>FlatDomesticCalculatedInternational</d:ShippingType> 
    </d:ShippingInformation> 
</m:properties> 

我與查詢上述努力得到我們所需要的特定的子元素跟...共事。使用名稱空間標籤中的「:」似乎令人困惑。我想要做的是能夠查詢元素以獲取d:Id,d:UserId,d:Currentprice等的值。歡迎任何建議。

回答

0

示例代碼,

const string baseAddress = "http://ebayodata.cloudapp.net/"; 
EBayData ebay = new EBayData(new Uri(baseAddress)); 

var items = ebay.Execute<Item>(new Uri(baseAddress + "Items?search=1756-L65")); 
foreach (var item in items) 
{ 
    Console.WriteLine(item.Title); 
} 
+0

謝謝你RaghuRam。太棒了。這極大地簡化了我正在嘗試做的事情,並幫助我更好地理解Linq的語法。 – DougM 2013-03-20 17:09:32

1

嘗試使用WCF數據服務客戶端。它可以爲你生成代理類來簡化事情。請參閱文檔here。這樣做的自定義網址與生成WCF DS客戶

+0

謝謝你的建議。我只是嘗試嘗試使用此服務的WCF,但尚未成功。可能是因爲我對這個主題缺乏瞭解。在我的實驗中,eBay服務似乎沒有遵循典型的oData格式,因爲URI中需要搜索項,例如http://ebayodata.cloudapp.net/Items?search=1756-L65。 WCF似乎不接受這種格式。我可以在我的項目中建立服務參考並查看方法,但未能成功獲取結果。 – DougM 2013-03-18 20:28:38