E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Amazon File Maker from ASP.net
by Charles Carroll

Here is our code to use an Amazon Web service from ASP.net to take a crude XML file of my favorite book and and supplements it with details data and fetched from the Amazon Webservice.

This program converts the crude file
/experiments/data/favebooks.xml
into the improved and expanded:
/experiments/data/bookdetailsfromamazon.ver1.xml

   filename=/experiments/amazonmakefile/amazonmakefile.ver1.aspx

<Test Script Below>


<%@ Import Namespace="System.data"%>
<%@ Import Namespace="System.XML"%>
<%@ Import Namespace="system.xml.xmlnodetype"%>
<%@ Import Namespace="System.XML.Serialization"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="AmazonSearchService"%>
<%@ Assembly src="/experiments/amazonws/amazonsearch.vb" %>
<script language="VB" runat="server">
Sub Page_Load(S As Object, E As EventArgs)
    DIM strFileInput as string=server.mappath("/experiments/data/favebooks.xml")
    DIm strFileOutputWeb as string="/data/bookdetailsfromamazon.ver1.xml"
    DIM strFileOutput as string=request.PhysicalApplicationPath  & strFileOutputWeb
    strFileOutputWeb = "/experiments/" & strFileOutputWeb
    hyp1.NavigateURL=strFileOutputWeb
    hyp1.Text=strFileOutputWeb
    
    DIM strXML as string
    DIM Xmltr1 as XMLTextReader
    DIM XmltrAmazonDetails as XMLTextReader
    DIM xmltw1 as XmlTextWriter
    DIM strISBN as string=""
    DIM strISBNprev as string=""
    DIM strKeyword as string
    DIM strComment as string
    DIM strQuote as string    
    
    TRY
        Xmltr1=new XMLtextReader(strFileInput)
        xmltw1=new XMLtextWriter(strFileOutput,encoding.utf8)
        
        xmltw1.Formatting=Formatting.Indented
        xmltw1.Indentation=8
        
        xmltw1.WriteStartDocument()
        xmltw1.WriteStartElement("BookDetailsFromAmazon")
        
        DO WHILE Xmltr1.Read()
            'trace.write("nodetype=" & XMLtr1.nodetype.tostring())
            'trace.write("nodename=" & XMLtr1.name)
            
            IF XmlTr1.nodeType=Element AND Xmltr1.Name="group"
                 Xmltr1.MoveTonextAttribute()
                 strKeyword= Xmltr1.Value()
                 ' trace.write("strKeyword", strKeyword)
            END IF
        
            IF XmlTr1.nodeType=Element AND Xmltr1.Name="isbn"
                Xmltr1.Read()
                strISBNprev=strISBN
                strISBN=XmlTr1.Value
                xmltw1.WriteStartElement("book")
                    ' now we can write the nodes
                    xmltw1.WriteElementString("isbn",strISBN)
                    xmltw1.WriteElementString("keyword",strKeyword)                

                    ' Amazon Time Start
                    IF strISBN.length=0
                        ' nothing to do, no ISBN
                        trace.warn("Missing ISBN", "strISBNprev=" & strISBNprev)
                    ELSE
                        XmltrAmazonDetails=new XMLtextReader(new StringReader(ISBNDetails(strISBN)))
                        DO WHILE XmltrAmazonDetails.Read()
                            ' Now we extract the authors
                            IF XmltrAmazonDetails.nodeType=Element AND XmltrAmazonDetails.Name="Authors"
                                DO UNTIL XmltrAmazonDetails.nodeType=EndElement AND XmltrAmazonDetails.Name="Authors"
                                    XmltrAmazonDetails.Read()
                                    'trace.write("Amazon Details value",XmltrAmazonDetails.Value)
                                    'trace.write("Amazon NodeType",XmltrAmazonDetails.NodeType.ToString())
                                    IF XmltrAmazonDetails.NodeType=system.xml.xmlnodetype.Text
                                        xmltw1.WriteElementString("author",XmltrAmazonDetails.Value)
                                    END IF
                                LOOP
                            END IF
                            IF XmltrAmazonDetails.Name="ProductName" AND XmltrAmazonDetails.nodeType=Element
                                    XmltrAmazonDetails.Read()
                                    xmltw1.WriteElementString("title",XmltrAmazonDetails.Value)
                            END IF
                            IF XmltrAmazonDetails.Name="Manufacturer" AND XmltrAmazonDetails.nodeType=Element
                                    XmltrAmazonDetails.Read()
                                    xmltw1.WriteElementString("Publisher",XmltrAmazonDetails.Value)
                            END IF
                            IF XmltrAmazonDetails.Name="OurPrice" AND XmltrAmazonDetails.nodeType=Element
                                    XmltrAmazonDetails.Read()
                                    xmltw1.WriteElementString("AmazonPrice",XmltrAmazonDetails.Value)
                            END IF
                            IF XmltrAmazonDetails.Name="ListPrice" AND XmltrAmazonDetails.nodeType=Element
                                XmltrAmazonDetails.Read()
                                xmltw1.WriteElementString("ListPrice",XmltrAmazonDetails.Value)
                            END IF
                            IF XmltrAmazonDetails.Name="ReleaseDate" AND XmltrAmazonDetails.nodeType=Element
                                XmltrAmazonDetails.Read()
                                xmltw1.WriteElementString("PublishDate",XmltrAmazonDetails.Value)
                            END IF                        
                        LOOP
                        ' Amazon Time End                        
                    END IF

            END IF
            
            ' Now we have tricky part since 0-n quote(s) and 0-1 comment1 may be attached to a book
            IF XmlTr1.nodeType=Element AND Xmltr1.Name="comment"
                XMLTr1.read()
                xmltw1.WriteElementString("commentsbychaz",Xmltr1.Value)
            END IF
            
            IF XmlTr1.nodeType=Element AND Xmltr1.Name="quote"
                XMLTr1.read()
                xmltw1.WriteElementString("quote",Xmltr1.Value)
            END IF
            
            ' Now we have tricky part since 0-n quote(s) and 0-1 comment1 may be attached to a book
            'trace.write("strISBN=",strISBN)
            'trace.write("strISBNPrev=",strISBNPrev)
            IF XmlTr1.nodeType=EndElement AND Xmltr1.Name="book"
                xmltw1.WriteFullEndElement ' book
            END IF
        LOOP
        
        ' last transfer done!
        xmltw1.WriteFullEndElement ' BookDetailsFromAmazon
        xmltw1.WriteEndDocument()
    CATCH exc1 as exception
        Page.Controls.Add(New literalcontrol(exc1.ToString() & exc1.StackTrace()))
    FINALLY
        If Not (XMLtr1 Is Nothing)
            XMLtr1.close()
        END IF
        
        If Not (XmltrAmazonDetails Is Nothing)
            XmltrAmazonDetails.close()
        End If
        
        If Not (xmltw1 Is Nothing)
            xmltw1.flush()
            xmlTw1.close()
        End if
    END TRY

End Sub


function ISBNdetails(strParmISBN as string) as string
    IF strParmISBN.length=0 THEN
        return("")
    END IF
    DIM proxy AS new AmazonSearchService()
    DIM reqamz1 as new AsinRequest()
    reqamz1.asin = strParmISBN
    reqamz1.type = "lite"
    reqAmz1.tag="learnasp"
    ' My Developer Token
    reqamz1.devtag = "D1967KF255R5KZ"

    DIM p1 as ProductInfo
    
    p1=proxy.AsinSearchRequest(reqamz1)
    'trace.warn("type",typename(p1.Details.GetType()))

    return(SerializeTypetoXML(p1.Details))
end function

function SerializeTypeToXML(objparm as object) as string
    ' now we convert that type into XML
    ' http://aspalliance.com/shark/articles/amzws/amzdoc.aspx
     DIM XmlSrlz1 as xmlSerializer
     XmlSrlz1=new XmlSerializer(objParm.GetType())
     DIM strwrtr1 as StringWriter = new StringWriter()
     XmlSrlz1.Serialize(strwrtr1, objParm)
     return(strwrtr1.ToString())
end function
</script>
<html><head>
<title>XML Demo</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
Created File: <ASP:hyperlink id="hyp1" runat="server" />
</form>
</body></html>

The file below is the refactored version improved to be more maintainable and easy to read:

   filename=/experiments/amazonmakefile/amazonmakefile.ver2.aspx

<Test Script Below>


<%@ Import Namespace="System.data"%>
<%@ Import Namespace="System.XML"%>
<%@ Import Namespace="system.xml.xmlnodetype"%>
<%@ Import Namespace="System.XML.Serialization"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="LearnAsp"%>
<%@ Import Namespace="AmazonSearchService"%>
<%@ Assembly src="/experiments/amazonws/amazonsearch.vb" %>
<script language="VB" runat="server">
Sub Page_Load(S As Object, E As EventArgs)
    DIM strFileInput as string=server.mappath("/experiments/data/favebooks.xml")
    DIM strFileOutputWeb as string="/data/bookdetailsfromamazon.ver1.xml"
    DIM strFileOutput as string=request.PhysicalApplicationPath  & strFileOutputWeb
    strFileOutputWeb = "/experiments/" & strFileOutputWeb
    hyp1.NavigateURL=strFileOutputWeb
    hyp1.Text=strFileOutputWeb

    
    DIM strXML as string
    DIM Xmltr1 as XMLTextReader
    DIM XmltrAmazonDetails as XMLTextReader
    DIM xmltw1 as XmlTextWriter
    DIM strISBN as string=""
    DIM strISBNprev as string=""
    DIM strKeyword as string
    DIM strComment as string
    DIM strQuote as string
    
    DIM strNodeType as string
    DIM strNodeName as string
    DIM strNodeValue as string
    
    TRY
        Xmltr1=new XMLtextReader(strFileInput)
        xmltw1=new XMLtextWriter(strFileOutput,encoding.utf8)
        
        xmltw1.Formatting=Formatting.Indented
        xmltw1.Indentation=8
        
        xmltw1.WriteStartDocument()
        xmltw1.WriteStartElement("BookDetailsFromAmazon")
        
        DO WHILE Xmltr1.Read()
            'trace.write("nodetype=" & XMLtr1.nodetype.tostring())
            'trace.write("nodename=" & XMLtr1.name)
            strNodeType=XmlTr1.nodeType.ToString
            strNodeValue=XmlTr1.Value
            strNodeName=XmlTr1.Name
            
            IF XmlTr1.nodeType=Element
                SELECT CASE strNodeName
                    CASE "group"
                         Xmltr1.MoveTonextAttribute()
                         strKeyword= Xmltr1.Value()
                         ' trace.write("strKeyword", strKeyword)
                     CASE "keyword"
                        Xmltr1.Read()
                        xmltw1.WriteElementString("keyword",XmlTr1.Value)
                     CASE "isbn"
                        Xmltr1.Read()
                        strISBNprev=strISBN
                        strISBN=XmlTr1.Value
                        xmltw1.WriteStartElement("book")
                        ' now we can write the nodes
                        xmltw1.WriteElementString("isbn",strISBN)
                        xmltw1.WriteElementString("keyword",strKeyword)                

                        ' Amazon Time Start
                        IF strISBN.length=0
                            ' nothing to do, no ISBN
                            trace.warn("Missing ISBN", "strISBNprev=" & strISBNprev)
                        ELSE
                            XmltrAmazonDetails=new XMLtextReader(new StringReader(ISBNDetails(strISBN)))
                            DO WHILE XmltrAmazonDetails.Read()
                                ' Now we extract the authors
                                IF XmltrAmazonDetails.nodeType=Element
                                    DIM strNodeNewName as string=""
                                    SELECT CASE XMLTrAmazonDetails.Name
                                    CASE "Authors"
                                        DO UNTIL XmltrAmazonDetails.nodeType=EndElement AND XmltrAmazonDetails.Name="Authors"
                                            XmltrAmazonDetails.Read()
                                            'trace.write("Amazon Details value",XmltrAmazonDetails.Value)
                                            'trace.write("Amazon NodeType",XmltrAmazonDetails.NodeType.ToString())
                                            IF XmltrAmazonDetails.NodeType=system.xml.xmlnodetype.Text
                                                xmltw1.WriteElementString("author",XmltrAmazonDetails.Value)
                                            END IF
                                        LOOP
                                    CASE "ProductName"
                                        strNodeNewName="title"
                                    CASE "Manufacturer"
                                        strNodeNewName="publisher"
                                    CASE "OurPrice"
                                        strNodeNewName="AmazonPrice"
                                    CASE "ListPrice"
                                        strNodeNewName="listprice"
                                    CASE "ReleaseDate"
                                        strNodeNewName="publishdate"
                                    END SELECT
                                    IF strNodeNewName<>""
                                        XmltrAmazonDetails.Read()
                                        xmltw1.WriteElementString(strNodeNewName,XmltrAmazonDetails.Value)
                                    END IF
                                END IF        
                            LOOP
                            ' Amazon Time End                        
                        END IF
                    CASE "comment"
                        XMLTr1.read()
                        xmltw1.WriteElementString("commentsbychaz",Xmltr1.Value)
                    CASE  "quote"
                        XMLTr1.read()
                        xmltw1.WriteElementString("quote",Xmltr1.Value)
                    END SELECT    

            END IF
            
            
            ' Now we have tricky part since 0-n quote(s) and 0-1 comment1 may be attached to a book
            'trace.write("strISBN=",strISBN)
            'trace.write("strISBNPrev=",strISBNPrev)
            IF XmlTr1.nodeType=EndElement AND Xmltr1.Name="book"
                xmltw1.WriteFullEndElement ' book
            END IF
        LOOP
        
        ' last transfer done!
        xmltw1.WriteFullEndElement ' BookDetailsFromAmazon
        xmltw1.WriteEndDocument()
    CATCH exc1 as exception
        Page.Controls.Add(New literalcontrol(exc1.ToString() & exc1.StackTrace()))
    FINALLY
        If Not (XMLtr1 Is Nothing)
            XMLtr1.close()
        END IF

        If Not (XmltrAmazonDetails Is Nothing)
            XmltrAmazonDetails.close()
        End If

        If Not (xmltw1 Is Nothing)
            xmltw1.flush()
            xmlTw1.close()
        End if
    END TRY

End Sub


function ISBNdetails(strParmISBN as string) as string
    IF strParmISBN.length=0 THEN
        return("")
    END IF
    DIM proxy AS new AmazonSearchService()
    DIM reqamz1 as new AsinRequest()
    reqamz1.asin = strParmISBN
    reqamz1.type = "lite"
    reqAmz1.tag="learnasp"
    ' My Developer Token
    reqamz1.devtag = "D1967KF255R5KZ"

    DIM p1 as ProductInfo
    
    p1=proxy.AsinSearchRequest(reqamz1)
    'trace.warn("type",typename(p1.Details.GetType()))

    return(SerializeTypetoXML(p1.Details))
end function

function SerializeTypeToXML(objparm as object) as string
    ' now we convert that type into XML
    ' http://aspalliance.com/shark/articles/amzws/amzdoc.aspx
     DIM XmlSrlz1 as xmlSerializer
     XmlSrlz1=new XmlSerializer(objParm.GetType())
     DIM strwrtr1 as StringWriter = new StringWriter()
     XmlSrlz1.Serialize(strwrtr1, objParm)
     return(strwrtr1.ToString())
end function
</script>
<html><head>
<title>XML Demo</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
Created File: <ASP:hyperlink id="hyp1" runat="server" />
</form>
</body></html>
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.