|
-
Aug 3rd, 2004, 11:18 PM
#1
Thread Starter
Fanatic Member
XML file searching and updating
Ok I'm learning this more and more every single day and I've about got it. But I have a couple questions.
I am now successfully able to load an xml document, point to a node, capture and display the value of that node, and change the value of the node, using this code:
Code:
Private Sub xmlChange()
Dim DS As New DataSet()
'loads xml file into dataset
DS.ReadXml("c:\inetpub\wwwroot\xmlTest\xmlemp.xml")
DS.Tables(0).Rows(0).Item("Name") = "NEW VALUE"
' write the xml file
DS.WriteXml("c:\inetpub\wwwroot\xmlTest\xmlemp.xml")
End Sub
As you can see, I'm using a dataset to accomplish the above.
Another way I'm trying this is with XPath, which I'm still learning. This code pulls the value of whatever node I choose, but I have to know the name of the node beforehand (i think):
Code:
Public Sub xmlRecord()
Dim xpathdoc As XPath.XPathDocument
Dim xmlNav As XPath.XPathNavigator
Dim xmlITI As XPath.XPathNodeIterator
xpathdoc = New XPath.XPathDocument("C:\Inetpub\wwwroot\xmlTest\xmlemp.xml")
xmlNav = xpathdoc.CreateNavigator
xmlITI = xmlNav.Select("/NewDataSet/Table/Department")
While (xmlITI.MoveNext())
xmlTextbox.Text = xmlITI.Current.Name + " : " + xmlITI.Current.Value
End While
End Sub
Now using these this method, I can pull any values I want as long as I know the node.
But what I am trying to accomplish is this:
I want to search an XML document, preferably WITHOUT knowing which node it will be in, then replace the value I'm looking for with another value I will have saved in a variable. Which of these two methods (dataset or XPaths) will be easier to accomplish this, and how can I search and replace the string I want without knowing which node it is in?
Thank you for all your help! I've learned alot about this in the past few days!
-
Aug 4th, 2004, 01:32 AM
#2
I don't have an answer, but two points:
Using the first method you have above, couldn't you loop through the nodes and look for the value you want?
Also, I believe that you might be looking for something related to XML Schemas. Once you can understand the schema, you can essentially loop through the document and know what you're looking for.
You mentioned that you can "successfully load an xml document, point to a node, capture and display the value of that node, and change the value of the node,"
I hope you've got the code for that saved, because I'm at the same point in learning as you, and would like to have a look at it as well, to compare. Show me?
-
Aug 4th, 2004, 04:27 AM
#3
Here's a small example, from a thread I posted a day or two ago:
VB Code:
Dim XMLDoc As New XmlDocument
Dim XMLN As XmlNode
XMLDoc.Load(Server.MapPath("family.xml"))
Dim XMLNL As XmlNodeList = XMLDoc.SelectNodes("//family/name")
For Each XMLN In XMLNL
If XMLN.ChildNodes(0).InnerText.ToUpper = "Alfred E".ToUpper Then
XMLN.ChildNodes(0).InnerText = "What me worry"
End If
Next
XMLDoc.Save(Server.MapPath("family.xml"))
Does this help?
-
Aug 4th, 2004, 12:28 PM
#4
Thread Starter
Fanatic Member
Yes that does help! ANd it makes sense, but here's my xml file:
Code:
<?xml version="1.0" standalone="yes" ?>
- <NewDataSet>
- <Table>
<Name>Kenny</Name>
<Department>Programming</Department>
<Salary>6500.65</Salary>
</Table>
- <Table>
<Name>TESTING</Name>
<Department>Analysis</Department>
<Salary>6000.45</Salary>
</Table>
- <Table>
<Name>%@Norton%</Name>
<Department>Antivirus</Department>
<Salary>7500.75</Salary>
</Table>
</NewDataSet>
and here's the code I use: but with no results when I run this
VB Code:
Public Sub xmlSearch()
Dim xmlFile As New XmlDocument()
Dim xmlNode As XmlNode
xmlFile.Load("c:\Inetpub\wwwroot\xmlTest\xmlemp.xml")
Dim xmlNL As XmlNodeList = xmlFile.SelectNodes("/NewDataSet/Table/Department")
For Each xmlNode In xmlNL
If xmlNode.ChildNodes(0).InnerText.ToUpper = "Programming" Then
xmlNode.ChildNodes(0).InnerText = "ProgrammingReplaced"
End If
Next
xmlFile.Save("c:\Inetpub\wwwroot\xmlTest\xmlemp.xml")
End Sub
When i call this to a button click, nothing happens, no error, and it doesn't change the value inside my department node.
Almost there I think Thanks for all your help man!
Last edited by drpcken; Aug 4th, 2004 at 12:32 PM.
-
Aug 4th, 2004, 12:32 PM
#5
Your If statement is a little off:
you have:
VB Code:
If xmlNode.ChildNodes(0).InnerText.ToUpper = "Programming" Then
It should be:
VB Code:
If xmlNode.ChildNodes(0).InnerText.ToUpper = "Programming".ToUpper Then
In your code, you upper cased the inner HTML, but not the text you're looking for, so "PROGRAMMING" <> "Programming" it never goes into the true case of the If.
TG
-
Aug 4th, 2004, 01:07 PM
#6
Thread Starter
Fanatic Member
PERFECT!
I didn't even need the toupper so i took it out all together.
Thank you guys for all your help!!!!!
I'm sure I'll have more questions soon!! And I mean soon as in today, once I start digging deeper into this...
-
Aug 5th, 2004, 12:23 AM
#7
drpcken: I'd still like to see the code you're using for your other functions. I'd like to compare it with mine. A lack of good tutorials out there, see? Is it possible for you to zip up the project and post it here? Or rather, post some code...
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
|