|
-
May 19th, 2009, 02:50 PM
#1
Thread Starter
Member
[RESOLVED] looping through XML file
Is there a way to loop through an XML file and retrieve desired data from elements and attributes?
I wrote a little app that is supposed to do this, but it only returns duplicate data from the first element it encounters.
Here is the code:
Code:
Dim REMdoc As New XmlDocument()
REMdoc.Load(path + "remodel.xml")
Dim REMnav As XPathNavigator = DAMdoc.CreateNavigator()
Dim SelectedNode As XPathNavigator
For Each SelectedNode In REMnav.[Select]("//ns1:reNode", nsmgr)
xmlNodeId = SelectedNode.SelectSingleNode("//ns1:reNode/@id", nsmgr).Value
xmlNodeTitle = SelectedNode.SelectSingleNode("//ns1:reNode/@label", nsmgr).Value
'xmlNodeContentPath = node.SelectSingleNode("//ns1:reNode/Content", nsmgr).Value
query = REMnav.Compile("//ns1:ActivityNode/Content")
Response.Write("<b>node Id:</b> " & xmlNodeId.ToString)
Response.Write("<br><b>node Title:</b> " & xmlNodeTitle.ToString & "<br>")
Response.Write("<b>node Content:</b> " & query.ToString & "<br><br>")
Response.Write(query.ToString & "<br>")
Next
Thanks!
-
May 20th, 2009, 02:13 AM
#2
Re: looping through XML file
Hey,
Can you post a sample of the XML that you are trying to parse, and also an explanation of what data exactly you are wanting to retrieve from it.
You should almost certainly be able to do what you want. It is just a matter of helping us understand what it is you are trying to achieve.
Gary
-
May 20th, 2009, 08:23 AM
#3
Thread Starter
Member
Re: looping through XML file
Certainly...
I am trying to retrieve all the attributes and element data(id, label, and content) and assign that data to a variable.
BTW, I had a little typo in the code I posted before. ActivityNode should be reNode.
I do get data, but it's just duplicate rows of this(which happens to be the first row in the XML file):
Code:
node Id: 2009040710585662
node Title: Start your case here
node Content: MS.Internal.Xml.XPath.CompiledXpathExpr
Here is a sample of the XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<reModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.er.org/remodel/v1/ ermodel.xsd" xmlns="http://ns.er.org/remodel/v1/">
<reNodes>
<NodeSection id="90c2fba4-a8cb-4273-bdb4-78bcc732f8c9" label="XML Test Case">
<reNode id="2009040710585662" label="Start your case here">
<Content>/DataAvailabilityModel/DAMNode[@id = 'd14']</Content>
</reNode>
<reNode id="2009040711003882" label="Space...the final frontier">
<Content>/DataAvailabilityModel/DAMNode[@id = 'd15']</Content>
</reNode>
<reNode id="9050810371006880" label="MCQ Node # 3">
<Content>/DataAvailabilityModel/DAMNode[@id = 'd32']</Content>
</reNode>
<reNode id="9050810442203492" label="Buying a Mac">
<Content>/DataAvailabilityModel/DAMNode[@id = 'd33']</Content>
</reNode>
<reNode id="9050810442403804" label="Buying a PC">
<Content>/DataAvailabilityModel/DAMNode[@id = 'd34']</Content>
</reNode>
<reNode id="9050810442605996" label="Buying an iPhone">
<Content>/DataAvailabilityModel/DAMNode[@id = 'd35']</Content>
</reNode>
<reNode id="9050810442905058" label="Buying a Blackberry">
<Content>/DataAvailabilityModel/DAMNode[@id = 'd36']</Content>
</reNode>
</NodeSection>
</reNodes>
</reModel>
Thanks!
-
May 20th, 2009, 08:46 AM
#4
Re: looping through XML file
Hey,
What version of Visual Studio are you using? If you are using 2008, then you may be able to do something with LINQ to XML, I don't know too much about it, as I haven't used it yet, but you can find some information about it here:
http://weblogs.asp.net/scottgu/archi...r-with-it.aspx
Having said that, one way of doing it would be as follows....
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim REMdoc As New XmlDocument()
REMdoc.Load("XMLFile1.xml")
Dim mngr As New XmlNamespaceManager(REMdoc.NameTable)
mngr.AddNamespace("ns1", "http://ns.er.org/remodel/v1/")
Dim NodeSection As XmlNode = REMdoc.SelectSingleNode("/ns1:reModel/ns1:reNodes/ns1:NodeSection", mngr)
MessageBox.Show(NodeSection.Attributes("id").Value)
MessageBox.Show(NodeSection.Attributes("label").Value)
Dim reNodes As XmlNodeList = NodeSection.SelectNodes("//ns1:reNode", mngr)
For Each node As XmlNode In reNodes
MessageBox.Show(node.Attributes("id").Value)
MessageBox.Show(node.Attributes("label").Value)
Dim ContentNode As XmlNode = node.ChildNodes(0)
MessageBox.Show(ContentNode.InnerText)
Next
End Sub
Bear in mind, this was done in a Windows Forms Application, hence the MessageBox's, but the approach would be the same in a Web Application.
This is just one way of parsing the XML, there are various ways to do it, this is just one way that came to mind based on what you have asked for.
Gary
-
May 20th, 2009, 08:57 AM
#5
Thread Starter
Member
Re: looping through XML file
Wow...that worked!
Thanks! What was I doing wrong? I had a For/Next loop in my code...
-
May 20th, 2009, 09:00 AM
#6
Re: [RESOLVED] looping through XML file
Hey,
To be honest, I didn't really look at your code, I am not familiar with the XPathNavigator as I have never used it, I simply got it to work using a technique that I am familiar with 
Someone may come along and be able to point out what was wrong with your original posting.
Gary
-
May 20th, 2009, 09:07 AM
#7
Thread Starter
Member
Re: [RESOLVED] looping through XML file
-
May 25th, 2009, 06:40 AM
#8
Re: [RESOLVED] looping through XML file
Yes yes, I know this is resolved.
Using the XPathNavigator is almost the same as XmlDocument - but you'd use XPathNavigator in readonly situations when you don't want to modify anything (performance differences).
Also, don't use //, use a single-slash path to the nodes you want.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|