0

我正在使用他们的 odata 提要从 eBay 提取信息。我正在尝试使用 linq 来提取我们所追求的特定信息。使用 linq,我可以深入了解包含我们想要的信息的特定元素。我不能做的是弄清楚如何查询元素数据以获取我想要的特定子元素。我可以解析它,但真的很想学习 linq。我使用 vb.net 作为语言。要获得我在使用以下内容后的元素:

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 等的值。欢迎提出任何建议。

4

2 回答 2

1

尝试使用 WCF 数据服务客户端。它可以为您生成可以简化事情的代理类。请参阅此处的文档。

于 2013-03-14T02:01:39.123 回答
0

使用生成的 WCF DS 客户端执行自定义 url 的示例代码,

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);
}
于 2013-03-19T18:32:30.113 回答