Parsing Amazon XML using Linq

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart Shay

    Parsing Amazon XML using Linq

    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.

    <EditorialRevie ws>
    <EditorialRevie w>
    <Source>Produ ct Description</Source>
    <ContentProd desc ........</Content>
    </EditorialReview >
    <EditorialRevie w>
    <Source>Amazon. com Review</Source>
    <ContentAMZN Review desc *** This Text *****</Content /
    </EditorialReview >
    </EditorialReview s>


    //Call to Parse Editorial Reviews Nodes
    Notes = ParseNotes(resu lt.Descendants( ns + "EditorialRevie ws")),


    //Parse Editorial Reviews
    private string ParseNotes(IEnu merable<XElemen tnotesElement)
    {
    var productDescript ion = (from notes in
    notesElement.De scendants(ns + "EditorialRevie w")
    let source = (string) notes.Element(n s
    + "Source")
    where source == "Amazon.com Review"
    select new
    {
    Source = (string)
    notes.Element(n s + "Source"),
    Content = (string)
    notes.Element(n s + "Content")
    });



    foreach (var s in productDescript ion)
    {
    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





  • Martin Honnen

    #2
    Re: Parsing Amazon XML using Linq

    Stuart Shay wrote:
    I am working on parsing the Amazon Key word search webservice for the
    Editorial Reviews Content containing "Amazon.Com Review" in the Source
    element.
    >
    <EditorialRevie ws>
    <EditorialRevie w>
    <Source>Produ ct Description</Source>
    <ContentProd desc ........</Content>
    </EditorialReview >
    <EditorialRevie w>
    <Source>Amazon. com Review</Source>
    <ContentAMZN Review desc *** This Text *****</Content /
    </EditorialReview >
    </EditorialReview s>
    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 (@"<EditorialRe views>
    <EditorialRevie w>
    <Source>Produ ct Description</Source>
    <ContentProd desc ........</Content>
    </EditorialReview >
    <EditorialRevie w>
    <Source>Amazon. com Review</Source>
    <ContentAMZN Review desc *** This Text *****</Content /
    </EditorialReview >
    </EditorialReview s>");
    ns = doc.Root.Name.N amespace;

    ParseNotes(doc. Descendants(ns + "EditorialRevie ws"));
    }

    private static void ParseNotes(IEnu merable<XElemen tnotesElement)
    {
    var productDescript ion = (from notes in
    notesElement.De scendants(ns + "EditorialRevie w")
    let source = (string)
    notes.Element(n s + "Source")
    where source == "Amazon.com Review"
    select new
    {
    Source = (string)
    notes.Element(n s + "Source"),
    Content = (string)
    notes.Element(n s + "Content")
    });



    foreach (var s in productDescript ion)
    {
    Console.WriteLi ne(s.Content);

    }
    }
    }

    Output is

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

    without duplicates.




    --

    Martin Honnen --- MVP XML

    Comment

    Working...