Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds


  Asked By: Jason    Date: Jul 07    Category: Asp.net    Views: 681

When I first looked into using XML I thought to myself - what a tidy way of
moving data around.

No that I am trying to move some data around I am struggling like mad.

My problem is this:

I am designing a web base purchasing order system in ASP.NET(VB) that will
integrate with a suppliers database via an XML interface.

The first thing I am trying to do is read an XML file that gives me the
current stock level and estimated delivery date. (We have an 'upto one day
out of date' database of products, which is where I get my information from
first) I wish to use the XML interface to get the most upto date
infromation and display it to a user when the click on a relvent product

I get the information from the supplier by requesting an XML document over
the internet. I have been using the XMLTextReader with code base on the
tutorial from MSDN. I can get the code to laod the file and then display it
on a page. However, I don't seem to be able to link an element with it's
associated text. For example there is an element called Price and then a
text for the price.

I want to be able to add our mark up to the price before displaying it on
the web page and I am not sure if I can do this with XmlTextReader.

Should I be using some other method for reading XML via HTTP?

I am sorry if this all sounds confused - it is mostly likely sounding
confused because I am very confused!



1 Answer Found

Answer #1    Answered By: Corinne Rogers     Answered On: Jul 07

Below is the code  I am using. What I don't undersatnd is how I can get a
the value for each element  into a variable so I can do some work with them.

Imports Microsoft.VisualBasic
Imports System
Imports System.Xml

namespace HowTo.Samples.XML

public class XmlReadFromURLSample
dim CustIDc as string= ""

Dim prodidq as string= "639749"
'URL to read  from
dim localURL as String =
"http://intouch.computer2000.com/xml/xmlonl.asp?custid=325009"& "&" &
"prodid="& ProdIDq

public shared sub Main()
Dim myXmlReadFromURLSample as XmlReadFromURLSample = new
end sub

public sub Run()
Dim myXmlURLReader as XmlTextReader

' Reading xml  from a URL
Console.WriteLine ("Initializing XmlTextReader ...")
Console.WriteLine ("Reading from URL: {0}", localURL)

' Load the XmlTextReader from the URL
myXmlURLReader = new XmltextReader (localURL)

Console.WriteLine ("Processing ...")
Console.WriteLine ()

catch e as Exception

Console.WriteLine ("Exception: {0}", e.ToString())
Console.WriteLine ("Make sure that, {0} exists", localURL)


Console.WriteLine("Processing of URL complete.")
' Finished with XmlTextReader
If Not myXmlURLReader Is Nothing
End If

end try
end sub

private shared Sub FormatXml (reader as XmlTextReader)

Dim declarationCount, piCount, docCount, commentCount, elementCount
as Integer
Dim attributeCount, textCount, whitespaceCount as Integer
Dim Element as string
Dim text  as String
While reader.Read()

Select (reader.NodeType)

case XmlNodeType.XmlDeclaration:
' Format (reader, "XmlDeclaration")
declarationCount += 1

case XmlNodeType.ProcessingInstruction:
' Format (reader, "ProcessingInstruction")
piCount += 1

case XmlNodeType.DocumentType:
' Format (reader, "DocumentType")
docCount += 1

case XmlNodeType.Comment:
' Format (reader, "Comment")
commentCount += 1

case XmlNodeType.Element:
Format (reader, "Element")
elementCount += 1
if (reader.HasAttributes)
attributeCount += reader.AttributeCount
end if

case XmlNodeType.Text:
Format (reader, "Text")
textCount += 1

case XmlNodeType.Whitespace:
whitespaceCount += 1

End Select

End While

' Display the Statistics for the file
'Console.WriteLine ()
'Console.WriteLine("Statistics for URL")
'Console.WriteLine ()
'Console.WriteLine("XmlDeclaration: " & declarationCount)
'Console.WriteLine("ProcessingInstruction: " & piCount)
'Console.WriteLine("DocumentType: " & docCount)
'Console.WriteLine("Comment: " & commentCount)
'Console.WriteLine("Element: " & elementCount)
'Console.WriteLine("Attribute: " & attributeCount)
'Console.WriteLine("Text: " & textCount)
'Console.WriteLine("Whitespace: " & whitespaceCount)
End Sub

private shared Sub Format(byref reader as XmlTextReader , nodeType as

' Format the output
'Console.Write(reader.Depth & " ")
'Console.Write(reader.AttributeCount & " ")

Dim i as Integer
for i = 0 to reader.Depth - 1

'if reader.name = "DESC" then
Console.Write("1:" & reader.Name & VBCRLF & "2:" & reader.Value)
'end if

'Display the attributes values for the current node
'if (reader.HasAttributes)
'Console.Write(" Attributes:")
'Dim j as Integer
'for j = 0 to reader.AttributeCount - 1
'Console.Write(" [{0}] " & reader(j), j)
'End if


End Sub

end Class 'XMLNodeReaderSample
end Namespace 'HowTo.Samples.XML

----------------------------ASPX file----------

<%@ Page Language="VB" Debug="true" Src="XmlReadFromUrl.vb"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="HowTo.Samples.XML" %>

<link rel="stylesheet" href="intro.css">
<body style="background-color:f6e4c6">

<form action="XmlReadFromUrl.aspx" method="post" runat="server">
<font>Input file: </font>
<a href="server/xml/books.xml">books.xml</a>
    <asp:button type=submit text="Run"
OnClick="SubmitBtn_Click" runat="server"/></br>

<table align="center">
<tr><th><span>Output from loading file...</span></th></tr>
<tr><td><h4><xmp id="output" runat="server"/></h4></td></tr>


<script language="VB" runat="server">

Private Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Dim writer as StringWriter
writer = new StringWriter()
Dim myXmlReadFromUrlSample as XmlReadFromUrlSample
myXmlReadFromUrlSample = new XmlReadFromUrlSample()
output.InnerHtml = writer.ToString()
End Sub


Didn't find what you were looking for? Find more on XM-HELL Or get search suggestion and latest updates.

Related Topics:



Related Post