Results 1 to 7 of 7

Thread: XML file searching and updating

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    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!

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Here's a small example, from a thread I posted a day or two ago:

    VB Code:
    1. Dim XMLDoc As New XmlDocument
    2.         Dim XMLN As XmlNode
    3.  
    4.         XMLDoc.Load(Server.MapPath("family.xml"))
    5.  
    6.         Dim XMLNL As XmlNodeList = XMLDoc.SelectNodes("//family/name")
    7.  
    8.         For Each XMLN In XMLNL
    9.  
    10.             If XMLN.ChildNodes(0).InnerText.ToUpper = "Alfred E".ToUpper Then
    11.                 XMLN.ChildNodes(0).InnerText = "What me worry"
    12.             End If
    13.  
    14.         Next
    15.  
    16.         XMLDoc.Save(Server.MapPath("family.xml"))

    Does this help?

  4. #4

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    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:
    1. Public Sub xmlSearch()
    2.  
    3.         Dim xmlFile As New XmlDocument()
    4.         Dim xmlNode As XmlNode
    5.  
    6.         xmlFile.Load("c:\Inetpub\wwwroot\xmlTest\xmlemp.xml")
    7.  
    8.         Dim xmlNL As XmlNodeList = xmlFile.SelectNodes("/NewDataSet/Table/Department")
    9.  
    10.         For Each xmlNode In xmlNL
    11.             If xmlNode.ChildNodes(0).InnerText.ToUpper = "Programming" Then
    12.                 xmlNode.ChildNodes(0).InnerText = "ProgrammingReplaced"
    13.             End If
    14.         Next
    15.  
    16.         xmlFile.Save("c:\Inetpub\wwwroot\xmlTest\xmlemp.xml")
    17.  
    18.     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.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Your If statement is a little off:

    you have:
    VB Code:
    1. If xmlNode.ChildNodes(0).InnerText.ToUpper = "Programming" Then

    It should be:
    VB Code:
    1. 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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    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...


  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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
  •  



Click Here to Expand Forum to Full Width