locked
How to change xml files node value using VB.Net? RRS feed

  • Question

  • Hi all,
            I want to change the value of a single node in an xml file using VB.Net. How can I do this without saving the entire XML file with the node value changed?

    thanks

    Q
    Saturday, September 27, 2008 8:42 PM

Answers

  •  

    You can edit an xml nodes value without save the whole file and without doing the SQL thing above.

     

    Original XML

     

    Code Snippet
     
      <?xml version="1.0" encoding="utf-8" ?>
    - <CryTools>
    - <Application>
      <AppPath>F:\Installs\CryTools</AppPath>
      <AppExe>F:\Installs\CryTools\CryTools.exe</AppExe>
      <Devmode>Yes</Devmode>
     </Application>
    - <FarCry>
      <GamePath>F:\Games\FarCry\Bin32\FarCry.exe</GamePath>
      <GameExe>F:\Games\FarCry\Bin32\FarCry.exe</GameExe>
      <LevelsFolder>F:\Games\FarCry\Levels</LevelsFolder>
     </FarCry>
    - <Crysis>
      <GamePath>F:\Games\Crysis</GamePath>
      <GameExe>F:\Games\Crysis\Bin32\Crysis.exe</GameExe>
      <LevelsFolder>F:\Games\Crysis\Game\Levels</LevelsFolder>
     </Crysis>
     </CryTools>

     

    Change the node value

    Code Snippet

    Dim MyXML As New XmlDocument()

    MyXML.Load("C:\XmlTest.xml")

    Dim MyXMLNode As XmlNode = MyXML.SelectSingleNode("/CryTools/FarCry")

    If MyXMLNode IsNot Nothing Then

    MyXMLNode.ChildNodes(0).InnerText = "F:\Games\CHANGED\Bin32\FarCry.exe"

    Else

    'Do whatever 

    End If ' Save the Xml.

    MyXML.Save("C:\XmlTest.xml")

     

     

    New output xml below, ie you've just change a node value

     

    Code Snippet
      <?xml version="1.0" encoding="utf-8" ?>
    - <CryTools>
    - <Application>
      <AppPath>F:\Installs\CryTools</AppPath>
      <AppExe>F:\Installs\CryTools\CryTools.exe</AppExe>
      <Devmode>Yes</Devmode>
     </Application>
    - <FarCry>
      <GamePath>F:\Games\CHANGED\Bin32\FarCry.exe</GamePath>
      <GameExe>F:\Games\FarCry\Bin32\FarCry.exe</GameExe>
      <LevelsFolder>F:\Games\FarCry\Levels</LevelsFolder>
     </FarCry>
    - <Crysis>
      <GamePath>F:\Games\Crysis</GamePath>
      <GameExe>F:\Games\Crysis\Bin32\Crysis.exe</GameExe>
      <LevelsFolder>F:\Games\Crysis\Game\Levels</LevelsFolder>
     </Crysis>
     </CryTools>

     

    Wednesday, October 22, 2008 10:12 PM
  • To achieve that you would need to use a database with XML support. With MS SQL Server 2005 or 2008 you have a database type xml to store and manipulate XML. See http://msdn.microsoft.com/en-us/library/ms189887(SQL.90).aspx

    Then the database system allows you to manipulate nodes in a column of type xml, see http://msdn.microsoft.com/en-us/library/ms187093(SQL.90).aspx

     

    Sunday, September 28, 2008 11:46 AM

All replies

  • You answered your own question. That's the biggest headache with xml, you can't update.

     

    You can only read it and recreate it.

     

    A.D.T.

     

    Saturday, September 27, 2008 8:59 PM
  • To achieve that you would need to use a database with XML support. With MS SQL Server 2005 or 2008 you have a database type xml to store and manipulate XML. See http://msdn.microsoft.com/en-us/library/ms189887(SQL.90).aspx

    Then the database system allows you to manipulate nodes in a column of type xml, see http://msdn.microsoft.com/en-us/library/ms187093(SQL.90).aspx

     

    Sunday, September 28, 2008 11:46 AM
  •  

    You can edit an xml nodes value without save the whole file and without doing the SQL thing above.

     

    Original XML

     

    Code Snippet
     
      <?xml version="1.0" encoding="utf-8" ?>
    - <CryTools>
    - <Application>
      <AppPath>F:\Installs\CryTools</AppPath>
      <AppExe>F:\Installs\CryTools\CryTools.exe</AppExe>
      <Devmode>Yes</Devmode>
     </Application>
    - <FarCry>
      <GamePath>F:\Games\FarCry\Bin32\FarCry.exe</GamePath>
      <GameExe>F:\Games\FarCry\Bin32\FarCry.exe</GameExe>
      <LevelsFolder>F:\Games\FarCry\Levels</LevelsFolder>
     </FarCry>
    - <Crysis>
      <GamePath>F:\Games\Crysis</GamePath>
      <GameExe>F:\Games\Crysis\Bin32\Crysis.exe</GameExe>
      <LevelsFolder>F:\Games\Crysis\Game\Levels</LevelsFolder>
     </Crysis>
     </CryTools>

     

    Change the node value

    Code Snippet

    Dim MyXML As New XmlDocument()

    MyXML.Load("C:\XmlTest.xml")

    Dim MyXMLNode As XmlNode = MyXML.SelectSingleNode("/CryTools/FarCry")

    If MyXMLNode IsNot Nothing Then

    MyXMLNode.ChildNodes(0).InnerText = "F:\Games\CHANGED\Bin32\FarCry.exe"

    Else

    'Do whatever 

    End If ' Save the Xml.

    MyXML.Save("C:\XmlTest.xml")

     

     

    New output xml below, ie you've just change a node value

     

    Code Snippet
      <?xml version="1.0" encoding="utf-8" ?>
    - <CryTools>
    - <Application>
      <AppPath>F:\Installs\CryTools</AppPath>
      <AppExe>F:\Installs\CryTools\CryTools.exe</AppExe>
      <Devmode>Yes</Devmode>
     </Application>
    - <FarCry>
      <GamePath>F:\Games\CHANGED\Bin32\FarCry.exe</GamePath>
      <GameExe>F:\Games\FarCry\Bin32\FarCry.exe</GameExe>
      <LevelsFolder>F:\Games\FarCry\Levels</LevelsFolder>
     </FarCry>
    - <Crysis>
      <GamePath>F:\Games\Crysis</GamePath>
      <GameExe>F:\Games\Crysis\Bin32\Crysis.exe</GameExe>
      <LevelsFolder>F:\Games\Crysis\Game\Levels</LevelsFolder>
     </Crysis>
     </CryTools>

     

    Wednesday, October 22, 2008 10:12 PM
  • Thanks
    Thursday, November 6, 2008 4:12 PM
  • Hello,

    Im trying to edit just a single node, I used the code:


    Dim MyXML As New XmlDocument()
    
    MyXML.Load("C:\XmlTest.xml")
    
    Dim MyXMLNode As XmlNode = MyXML.SelectSingleNode("/CryTools/FarCry")
    
    If MyXMLNode IsNot Nothing Then
    
    MyXMLNode.ChildNodes(0).InnerText = "F:\Games\CHANGED\Bin32\FarCry.exe"
    
    Else
    
    'Do whatever 
    
    End If ' Save the Xml.
    
    MyXML.Save("C:\XmlTest.xml")
    
    





    but that is more for something like this:













    <CryTools> - - <Application> <AppPath>F:\Installs\CryTools</AppPath> <AppExe>F:\Installs\CryTools\CryTools.exe</AppExe> <Devmode>Yes</Devmode> </Application> - - <FarCry> <GamePath>F:\Games\CHANGED\Bin32\FarCry.exe</GamePath> <GameExe>F:\Games\FarCry\Bin32\FarCry.exe</GameExe> <LevelsFolder>F:\Games\FarCry\Levels</LevelsFolder> </FarCry> - - <Crysis> <GamePath>F:\Games\Crysis</GamePath> <GameExe>F:\Games\Crysis\Bin32\Crysis.exe</GameExe> <LevelsFolder>F:\Games\Crysis\Game\Levels</LevelsFolder> </Crysis> </CryTools>

     









    However my XML file looks like this:






    <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Table> <Product> <Product_id>1</Product_id> <Product_name>Product 1</Product_name> <Product_price>1000</Product_price> </Product> <Product> <Product_id>2</Product_id> <Product_name>Product 2</Product_name> <Product_price>2000</Product_price> </Product> <Product> <Product_id>3</Product_id> <Product_name>Product 3</Product_name> <Product_price>3000</Product_price> </Product> <Product> <Product_id>4</Product_id> <Product_name>Product 4</Product_name> <Product_price>4000</Product_price> </Product> </Table>






    how do I edit, product_id 3 (want to change the name 'product_name' for the 3rd one.)

    however this list (xml file) might have 200 <product> tags in it. (product_id might go to 200)

    I need a way of editing the product_name field.

    Any ideas how to do this? using the code above only does it to the first one and i need to be able to select which one to do it to.

    Hope someone can help

    Thanks.
    • Edited by --Aaron-- Monday, July 20, 2009 4:24 AM
    Monday, July 20, 2009 4:16 AM
  • The methods SelectSingleNode and SelectNodes take an XPath 1.0 expression so if you want to use that to find nodes in your XML you need to learn XPath:
    Dim productId As Integer  = 3
    Dim doc As New XmlDocument()
    doc.Load("file.xml")
    Dim pName As XmlNode = doc.SelectSingleNode(String.Format("Table/Product[Product_id = '{0}']/Product_name", productId))
    If pName IsNot Nothing Then
      pName.InnerText = "foo"
      doc.Save("file.xml")
    End If

    Note that with .NET 3.5 you can use LINQ to XML instead of XmlDocument and XPath.

    MVP XML My blog
    Monday, July 20, 2009 9:53 AM
  • Thanks.

    The code works fine.

    Thanks Heaps.
    Tuesday, July 21, 2009 1:01 AM
  • i have xml file :

    <?xml version="1.0" standalone="yes"?>

    <NewDataSet>

      <Table>

        <id_user>3789</id_user>

        <name>kiting</name>

      </Table>

      <Table>

        <id_user>3801</id_user>

        <name>dhika</name>

      </Table>

      <Table>

        <id_user>3803</id_user>

        <name>q-think</name>

      </Table>

      <Table>

        <id_user>3804</id_user>

        <name>admin user</name>

      </Table>

    </NewDataSet>

     

    so, i modify your code like:

    Dim productId As Integer  = 3
    Dim doc As New XmlDocument()
    doc.Load("file.xml")
    Dim pName As XmlNode = doc.SelectSingleNode(String.Format("NewDataSet/Table[id_user = '{0}']/name", productId))
    If pName IsNot Nothing Then
      pName.InnerText = "foo"
      doc.Save("file.xml")
    End If

     

    but pName is NOTHING.

    can someone help me?

    Tuesday, June 28, 2011 7:20 PM
  • i have xml file :

    <?xml version="1.0" standalone="yes"?>

    <NewDataSet>

      <Table>

        <id_user>3789</id_user>

        <name>kiting</name>

      </Table>

      <Table>

        <id_user>3801</id_user>

        <name>dhika</name>

      </Table>

      <Table>

        <id_user>3803</id_user>

        <name>q-think</name>

      </Table>

      <Table>

        <id_user>3804</id_user>

        <name>admin user</name>

      </Table>

    </NewDataSet>

     

    so, i modify your code like:

    Dim productId As Integer  = 3
    Dim doc As New XmlDocument()
    doc.Load("file.xml")
    Dim pName As XmlNode = doc.SelectSingleNode(String.Format("NewDataSet/Table[id_user = '{0}']/name", productId))
    If pName IsNot Nothing Then
      pName.InnerText = "foo"
      doc.Save("file.xml")
    End If

     

    but pName is NOTHING.

    can someone help me?

    The "[id_user = '{0}'] is to get the attributes but you have given it as inner text.

    With Regards, Raamakrishnan

    Wednesday, April 17, 2013 1:11 PM
  • You were the first person I've seen post this answer.  After a 4 1/2 hours of trying all the ways people have said to update XML I think I will try your method and just rebuild the document.  Bit of a pain but clearly updating nodes and stuff doesn't actually work the way they say it should.  Even the replace child does nothing 
    Monday, August 19, 2013 8:36 PM