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">/image s/logo.gif </property >
</userControl>
<userControl name="basicTableControl" order="2">
<property name="bgColor">blue</prope rty>
<property name="tableWidth">500px</p roperty>
</userControl>
<userControl name="basicTableControl" order="3">
<property name="footerText">Copyrigh t 2005</property>
</userControl>
</document>
I am using .net framework 2.0 with visual studio 2005 beta.
<document>
<userControl name="standardHeader" order="1">
<property name="headerText">This is a test header!</property>
<property name="logoLocation">/image
</userControl>
<userControl name="basicTableControl" order="2">
<property name="bgColor">blue</prope
<property name="tableWidth">500px</p
</userControl>
<userControl name="basicTableControl" order="3">
<property name="footerText">Copyrigh
</userControl>
</document>
I am using .net framework 2.0 with visual studio 2005 beta.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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?
ASKER
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?
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?
ASKER
Where is the code for the "SetProperty" part? How does that work?
see here:
c.SetProperty(nP.Attribute s("name"). Value, nP.InnerText)
see here:
c.SetProperty(nP.Attribute
ASKER
This seems to be the line that is causing the problem:
Dim nDoc As System.Xml.XmlDocument = xmld.ChildNodes(0)
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.
//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...
ASKER
I got it working by using a different technique. Thanks.
this is made in vs2003 -ntf 1.1 .... but try it !
Private Sub Get_Sourcedata()
Dim reader As XmlTextReader = New System.Xml.XmlTextReader("
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("//cu
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.GetNamedIt
dr(1) = node.Attributes.GetNamedIt
dtbl.Rows.Add(dr)
Next
dgvalutakurser.DataSource = dv
dgvalutakurser.DataBind()
End Sub