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

VB.net - XML Parser by Charles Carroll

Here is the XML Parser:

   filename=/experiments/xmlparser/xmlparser_vb.aspx

<Test Script Below>


<%@ Assembly src="/experiments/utilitybelt/vercurrent/utilitybelt.vb" %>
<%@ trace="true" debug ="true"%>
<%@ import Namespace="System.Xml"%>
<%@ import Namespace="System.IO"%>
<%@ import Namespace="LearnASP"%>
<script language="VB" runat="server">
    DIM ub1 as new UtilityBelt()
    
sub Page_Load(S as Object, E as EventArgs)
    Trace.TraceMode = TraceMode.SortByTime
    
    dim htl as new Hashtable()
    htl.Add("asin","0932956203")
    htl.Add("type","heavy")
    dim strAmazonResult as string = ub1.wsAmazonASINSearch(htl)
    

    ' XmlParse_v1(strAmazonResult)

    ' XmlParse_v2(strAmazonResult)
        
    ' XmlParse_v3(strAmazonResult,"ProductName,ListPrice,OurPrice,UsedPrice,Author,Rating")


    DIM HtAmazon1 as new Hashtable
    HtAmazon1=XmlParse_v4(strAmazonResult,"ProductName,ListPrice,OurPrice,UsedPrice,Author,Rating")
    HTPrinter(HtAmazon1)


    litAmazon.Text=Server.HtmlEncode(strAmazonResult)
end sub


sub XmlParse_v1(strAmazonResult as string)
    dim xmlrt1 as new XmlTextReader(new StringReader(strAmazonResult))
    DO WHILE xmlrt1.Read()
        ' Trace.Write("Node Type", xmlrt1.NodeType.ToString())
        dim strNodeType as string=xmlrt1.NodeType.ToString()
        dim strName as string=xmlrt1.Name
        
        if strNodeType="Element" AND strName="OurPrice"
            xmlrt1.Read()
            Trace.Write("OurPrice", xmlrt1.Value)
        end if
            
        if strNodeType="Element" AND strName="AvgCustomerRating"
            xmlrt1.Read()
            Trace.Write("average customer rating",xmlrt1.Value)
        end if

        if strNodeType="Element" AND strName="Author"
            xmlrt1.Read()
            Trace.Write("author",xmlrt1.Value)
        end if
    LOOP
END SUB


sub XmlParse_v2(strAmazonResult as string)
    dim xmlrt1 as new XmlTextReader(new StringReader(strAmazonResult))
    DO WHILE xmlrt1.Read()
        ' Trace.Write("Node Type", xmlrt1.NodeType.ToString());
        if xmlrt1.NodeType.ToString()="Element"
            select case xmlrt1.Name
                case "OurPrice"
                    xmlrt1.Read()
                    Trace.Write("our price", xmlrt1.Value)
                case "AvgCustomerRating"
                    xmlrt1.Read()
                    Trace.Write("average customer rating",xmlrt1.Value)
                case "Author"
                    xmlrt1.Read()
                    Trace.Write("author",xmlrt1.Value)
                  end select
        end if          
       LOOP
end sub


sub XmlParse_v3(strAmazonResult as string, strEleName as string)
        strEleName="," + strEleName+ ","
        DIM xmlrt1 as new XmlTextReader(new StringReader(strAmazonResult))
        DO WHILE xmlrt1.Read()
            if xmlrt1.NodeType.ToString()="Element"
                dim strName as string=xmlrt1.Name
                     if strEleName.IndexOf("," & strName & ",")>-1
                     xmlrt1.Read()
                         Trace.Write(strName,xmlrt1.Value)
                     end if
                 end if
               LOOP
end sub


function XmlParse_v4(strAmazonResult as string, strEleName as string) as hashtable
    strEleName="," & strEleName & ","
    dim ht as new Hashtable()
    DIm xmlrt1 as new XmlTextReader(new StringReader(strAmazonResult))
    dim i as integer=0
    do while xmlrt1.Read()
            if xmlrt1.NodeType.ToString()="Element"
                dim strName as string=xmlrt1.Name
                if (strEleName.IndexOf("," & strName & ",")>-1)
                    xmlrt1.Read()
                        if Not (ht.Contains(strName))
                        ht.Add(strName,xmlrt1.Value)
                            i=1
                    ' or as Jeff SchoolCraft would do it...
                    'if( xmlrt1.NodeType.ToString().Equals("Element") )  
                    '    {  
                    '    string strName=xmlrt1.Name;  
                    '    re = new Regex(strName); 
                    '    if(re.IsMatch(strEleName))
                    else
                        i+=1
                        ht.Add(strName & (i).ToString(),xmlrt1.Value)
                    end if
                end if
            end if
    loop
    return ht
end function

sub HTPrinter(htParm as hashtable)
    dim d as object
     for each d in htParm
        Trace.Write (d.Key.ToString(),d.Value.ToString())
    next
end sub
</script>
<html><head>
<title>XMLParser Tricks</title>
</head>
<body bgcolor="#FFFFFF">
<asp:literal id="litAmazon" EnableviewState="false" runat="server" />
<form 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.