[RESOLVED]Reading XML and getting Nodes

I’m having some trouble ready an xml file and getting the nodes. Here is the structure of the xml I’m working with:

<?xml version="1.0" ?>
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2013-08-01">
	<OperationRequest>
		<RequestId>94a1557f-239b-411f-b053-15e59b568e61</RequestId>
		<Arguments>
			<Argument Name="Operation" Value="ItemLookup"></Argument>
			<Argument Name="Service" Value="AWSECommerceService"></Argument>
			<Argument Name="Signature" Value="u1v4Mc="></Argument>
			<Argument Name="AssociateTag" Value="bbbb20"></Argument>
			<Argument Name="Version" Value="2013-08-01"></Argument>
			<Argument Name="ItemId" Value="B005VDX3ZK"></Argument>
			<Argument Name="IdType" Value="SKU"></Argument>
			<Argument Name="AWSAccessKeyId" Value=""></Argument>
			<Argument Name="Timestamp" Value="2014-11-10T19:16:07Z"></Argument>
			<Argument Name="ResponseGroup" Value="Medium"></Argument>
			<Argument Name="SearchIndex" Value="hhh"></Argument>
		</Arguments>
		<RequestProcessingTime>0.0286930000000000</RequestProcessingTime>
	</OperationRequest>
	<Items>
		<Request>
			<IsValid>True</IsValid>
			<ItemLookupRequest>
				<IdType>SKU</IdType>
				<ItemId>B005VDX3ZK</ItemId>
				<ResponseGroup>Medium</ResponseGroup>
				<SearchIndex>Toys</SearchIndex>
				<VariationPage>All</VariationPage>
			</ItemLookupRequest>
		</Request>
		<Item>
			<ASIN>B005VDX3ZK</ASIN>
			<ParentASIN>B00BEFJNQ2</ParentASIN>

I’m trying to get the Items/Item and I’m getting an error:
System.Collections.Generic.List`1[<>f__AnonymousType0`3[System.String,System.String,System.String]]

This is the code I’m working with to load the xml:

 var feed = XDocument.Load(requestUrl);
    var items = feed.Descendants("Items").Descendants("Item")
        .Select(item => new
    {
        Title   = item.Element("ASIN").Value,
        ASIN    = item.Element("ItemAttributes").Element("ASIN").Value,
        IMGAGE  = item.Element("MediumImage").Element("URL").Value
        
    });


-----

  @foreach (var item in items)
        {            
            <strong>@item.Title</strong>
            @item.ASIN<br />
            <img src="@item.IMGAGE" />
        }

You need to declare and initialise an XNamespace object:

 

XNamespace ns ="http://webservices.amazon.com/AWSECommerceService/2013-08-01";
var feed = XDocument.Load(yourDoc);

var items = feed.Descendants(ns + "Items").Descendants(ns +"Item")
    .Select(item => new
{
    Title   = item.Element(ns +"ASIN").Value,
    ASIN  = item.Element(ns +"ItemAttributes").Element(ns + "ASIN").Value
    //etc
});

Thanks Mike!

Is possible to test if the item node is null with Linq? For example if I’m passing a ItemId that doesn’t exist I receive the following response:

My first thought is to place a try catch and check if Item is null and check the errors > error, so I’m wonder if this could be accomplished in the Linq select?

<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2013-08-01">
  <OperationRequest>
    <RequestId>a2b04789-fa9c-4010-8ee8-404200a48508</RequestId>
    <Arguments>
      <Argument Name="Operation" Value="ItemLookup"></Argument>
      <Argument Name="Service" Value="AWSECommerceService"></Argument>
      <Argument Name="Signature" Value="000="></Argument>
      <Argument Name="AssociateTag" Value="000-20"></Argument>
      <Argument Name="Version" Value="2013-08-01"></Argument>
      <Argument Name="ItemId" Value="BBB"></Argument>
      <Argument Name="AWSAccessKeyId" Value="000"></Argument>
      <Argument Name="Timestamp" Value="2014-11-10T22:44:59Z"></Argument>
      <Argument Name="ResponseGroup" Value="Medium"></Argument>
    </Arguments>
    <RequestProcessingTime>0.0095860000000000</RequestProcessingTime>
  </OperationRequest>
  <Items>
    <Request>
      <IsValid>True</IsValid>
      <ItemLookupRequest>
        <IdType>ASIN</IdType>
        <ItemId>BBB</ItemId>
        <ResponseGroup>Medium</ResponseGroup>
        <VariationPage>All</VariationPage>
      </ItemLookupRequest>
      <Errors>
        <Error>
          <Code>AWS.InvalidParameterValue</Code>
          <Message>BBB is not a valid value for ItemId. Please change this value and retry your request.</Message>
        </Error>
      </Errors>
    </Request>
  </Items>
</ItemLookupResponse>

You can use FirstOrDefault:

var el = doc.Descendents("SomeElement").FirstOrDefault();
if(el != null){
    // it exists
}

Leave a Reply