Results 1 to 4 of 4

Thread: Update specific attibute in XML file.

  1. #1

    Thread Starter
    Lively Member hb21l6's Avatar
    Join Date
    Sep 2008
    Location
    UK - East Yorkshire
    Posts
    126

    Update specific attibute in XML file.

    Hi, I'm trying to update an XML file, but only a specific row.

    For instance in the xml sample below, there are four records, each with their own ID.
    I want to be able to update the record, where the ID = 4

    Code:
    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
      <returnset>
        <ID>3</ID>
    	<EJStatus />
      </returnset>
      <returnset>
        <ID>4</ID>
    	<EJStatus />
      </returnset>
      <returnset>
        <ID>5</ID>
    	<EJStatus />
      </returnset>
      <returnset>
        <ID>6</ID>
    	<EJStatus />
      </returnset>  
    </DocumentElement>
    This is the code sample I've been working with, but this only updates the first instance of EJstatus.

    Code:
        Public Sub SaveXMLFile()
    
            'Dim xmlDoc As New XmlDataDocumentxmlDoc.Load(dirPath & "jobs3.xml")
            Dim xmlDoc As New XmlDataDocument()
            xmlDoc.Load(dirPath & "jobs3.xml")
    
            Dim node As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/ID[. ='4']")
            Dim nodev As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/EJStatus")
    
            If node IsNot Nothing Then
                nodev.InnerText = "done"
            End If
    
            'For Each Attribute As XmlAttribute In xmlDoc.DocumentElement.Attributes
            '   If Attribute("ID").Value = 4 Then
            '     Attribute("EJStatus").InnerText = "read"
            '   End If
            ' Next
    
            xmlDoc.Save(dirPath & "jobs3.xml")
    
        End Sub
    I also tried looping each attribute to find the correct record for updating, but this never worked either.

    Any help on how to update the correct record would be much appreciated.

    Many thanks
    Dave

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update specific attibute in XML file.

    Code:
            Dim node As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/ID[. ='4']")
            Dim nodev As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/EJStatus")
    It's updating the first one, because that's what you told it to update... that second selectsinglenode pulls the first instance of the EJStatus it finds... but it's starting relative to the root... not the node where ID =4 ...
    what you need to do is start with the selected node (node) and find the sibling "EJStatus" ... which is easy enough because there's a NextSibling method...

    so what you end up with should be this (untested)
    Code:
        Public Sub SaveXMLFile()
    
            'Dim xmlDoc As New XmlDataDocumentxmlDoc.Load(dirPath & "jobs3.xml")
            Dim xmlDoc As New XmlDataDocument()
            xmlDoc.Load(dirPath & "jobs3.xml")
    
            Dim node As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/ID[. ='4']")
            Dim nodev As XmlNode = node.NextSibling()
    
            If nodev IsNot Nothing Then 'It's more important that nodev isn't nothing than it is node...
                nodev.InnerText = "done"
            End If
    
            xmlDoc.Save(dirPath & "jobs3.xml")
    
        End Sub
    This assumes that EJStatus is the next node following ID... if it isn't... you might need a small loop to check the .NextSibling until the node's name is the right one.

    -tg


    edit - your looping of the attributes didn't work because there are no attributes... those are all nodes...
    * 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??? *

  3. #3

    Thread Starter
    Lively Member hb21l6's Avatar
    Join Date
    Sep 2008
    Location
    UK - East Yorkshire
    Posts
    126

    Re: Update specific attibute in XML file.

    Thanks technome,
    nextsibling was very handy,, I did have to loop to find the correct node and update it..

    It works perfectly.

    Thanks again.
    Dave

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Update specific attibute in XML file.

    As an alternative, using Framework 3.5 or higher

    Code:
    Dim Identifier As Integer = 4
    Dim results = xmlDoc...<returnset> _
                  .FirstOrDefault(
                  Function(item) item.<ID>.Value = Identifier.ToString)
    
    If results IsNot Nothing Then
        results.<EJStatus>.Value = "Got it"
        ' show in the ide output window the element has changed
        Console.WriteLine(xmlDoc.ToString)
    End If
    output
    Code:
    <DocumentElement>
      <returnset>
        <ID>3</ID>
        <EJStatus />
      </returnset>
      <returnset>
        <ID>4</ID>
        <EJStatus>Got it</EJStatus>
      </returnset>
      <returnset>
        <ID>5</ID>
        <EJStatus />
      </returnset>
      <returnset>
        <ID>6</ID>
        <EJStatus />
      </returnset>
    </DocumentElement>

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