Parsing Amazon XML using Linq

  • Thread starter Thread starter Stuart Shay
  • Start date Start date
S

Stuart Shay

Hello All:

I am working on parsing the Amazon Key word search webservice for the
Editorial Reviews Content containing "Amazon.Com Review" in the Source
element.

<EditorialReviews>
<EditorialReview>
<Source>Product Description</Source>
<Content> Prod desc ........</Content>
</EditorialReview>
<EditorialReview>
<Source>Amazon.com Review</Source>
<Content> AMZN Review desc *** This Text *****</Content> /
</EditorialReview>
</EditorialReviews>


//Call to Parse Editorial Reviews Nodes
Notes = ParseNotes(result.Descendants(ns + "EditorialReviews")),


//Parse Editorial Reviews
private string ParseNotes(IEnumerable<XElement> notesElement)
{
var productDescription = (from notes in
notesElement.Descendants(ns + "EditorialReview")
let source = (string) notes.Element(ns
+ "Source")
where source == "Amazon.com Review"
select new
{
Source = (string)
notes.Element(ns + "Source"),
Content = (string)
notes.Element(ns + "Content")
});



foreach (var s in productDescription)
{
Response.Write(s.Content + "<hr/>");

}



When I loop through the results, the Content that I am looking for is
repeated 2x, how do I write the query so the correct results are only
displayed 1x.


Thanks
Stuart
 
Stuart said:
I am working on parsing the Amazon Key word search webservice for the
Editorial Reviews Content containing "Amazon.Com Review" in the Source
element.

<EditorialReviews>
<EditorialReview>
<Source>Product Description</Source>
<Content> Prod desc ........</Content>
</EditorialReview>
<EditorialReview>
<Source>Amazon.com Review</Source>
<Content> AMZN Review desc *** This Text *****</Content> /
</EditorialReview>
</EditorialReviews>
When I loop through the results, the Content that I am looking for is
repeated 2x, how do I write the query so the correct results are only
displayed 1x.

I can't reproduce the problem. Here is the code I constructed from your
posting:

class Program
{
private static XNamespace ns;
static void Main(string[] args)
{
XDocument doc = XDocument.Parse(@"<EditorialReviews>
<EditorialReview>
<Source>Product Description</Source>
<Content> Prod desc ........</Content>
</EditorialReview>
<EditorialReview>
<Source>Amazon.com Review</Source>
<Content> AMZN Review desc *** This Text *****</Content> /
</EditorialReview>
</EditorialReviews>");
ns = doc.Root.Name.Namespace;

ParseNotes(doc.Descendants(ns + "EditorialReviews"));
}

private static void ParseNotes(IEnumerable<XElement> notesElement)
{
var productDescription = (from notes in
notesElement.Descendants(ns + "EditorialReview")
let source = (string)
notes.Element(ns + "Source")
where source == "Amazon.com Review"
select new
{
Source = (string)
notes.Element(ns + "Source"),
Content = (string)
notes.Element(ns + "Content")
});



foreach (var s in productDescription)
{
Console.WriteLine(s.Content);

}
}
}

Output is

AMZN Review desc *** This Text *****

without duplicates.
 
Back
Top