Link to home
Start Free TrialLog in
Avatar of paulfryer
paulfryer

asked on

How to loop through an XML datasource?

I need to be able to loop through an xml document structure similar to this. Can someone please provide an ASP.NET (VB) example of how to do this. Basically, I just need to loop through each “userControl” element and get the attribute values out and the properties within each userControl.

<document>
    <userControl name="standardHeader" order="1">
        <property name="headerText">This is a test header!</property>
        <property name="logoLocation">/images/logo.gif</property>
    </userControl>
    <userControl name="basicTableControl" order="2">
        <property name="bgColor">blue</property>
        <property name="tableWidth">500px</property>
    </userControl>
    <userControl name="basicTableControl" order="3">
        <property name="footerText">Copyright 2005</property>
    </userControl>
</document>

I am using .net framework 2.0 with visual studio 2005 beta.

Avatar of vbturbo
vbturbo
Flag of Denmark image

Hi

this is made in vs2003 -ntf 1.1  .... but try it !

    Private Sub Get_Sourcedata()

        Dim reader As XmlTextReader = New System.Xml.XmlTextReader("http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml")

        Dim dtbl As New DataTable
        Dim dr As DataRow
        Dim dv As New DataView(dtbl)

        Dim objNodes As New System.Xml.XmlDocument
        objNodes.Load(reader)
        Dim oNodes As System.Xml.XmlNodeList
        oNodes = objNodes.SelectNodes("//currency")

        dtbl.Columns.Add(New DataColumn("Valuta", GetType(String)))

        dtbl.Columns.Add(New DataColumn("Kurs", GetType(String)))

        Dim node As System.Xml.XmlNode

        For Each node In oNodes

            dr = dtbl.NewRow()
            dr(0) = node.Attributes.GetNamedItem("desc").Value
            dr(1) = node.Attributes.GetNamedItem("rate").Value
            dtbl.Rows.Add(dr)

        Next


        dgvalutakurser.DataSource = dv

        dgvalutakurser.DataBind()

    End Sub
ASKER CERTIFIED SOLUTION
Avatar of kalliopi
kalliopi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paulfryer
paulfryer

ASKER

kalliopi,

I tried your code and got the following error:

Unable to cast object of type 'System.Xml.XmlElement' to type 'System.Xml.XmlDocument'.

any ideas?
What line are you getting this error on?
Here is what my source code looks like and the web page before I run the procedure you provided:

http://macrowebservices.com/webservice/SourceCodeAndPageBeforeRunn.gif

Here is the error I get once I run the procedure:

http://macrowebservices.com/webservice/afterProcedureHasRan.gif

Does that help?
Where is the code for the "SetProperty" part? How does that work?

see here:

 c.SetProperty(nP.Attributes("name").Value, nP.InnerText)
This seems to be the line that is causing the problem:
   Dim nDoc As System.Xml.XmlDocument = xmld.ChildNodes(0)
Have u tryed to load the xml file in u r browser and checked that it is loading proberly ? or that the path to file is correct....!
Yeah - if that's the line that's causing the problem it's probably because the XML is not being loaded correctly.  Is the TExtbox1.Text getting loaded with the right XML?  If so, that line should not be causing a problem, but I would debug it (set breakpoints and such) to see if it's loading the XML correctly...
As far as I can tell there really is a problem with this line, because xml.ChildNodes(0) is returning the root element (Document Element) of you xml document, which is an XmlElement not an XmlDocument:

//so instead of:
Dim nDoc As System.Xml.XmlDocument = xmld.ChildNodes(0)
//try using:
Dim nDoc As System.Xml.XmlElement = xmld.DocumentElement  // though xmld.ChildNodes(0) is probably also working.

Hope this helps.
I've tested the code with that XML.  If the XML gets loaded correctly, the code should work as written even though it's referencing the document element.  the XMLDocument class can handle a fragment or the full text of a well formed XML document.  I think it's more likely that the XML is not being loaded correctly...
I got it working by using a different technique. Thanks.