How can I edit values in an XML document?

Hi,

I am working on a project in Visual Studio using VB.NET and want to use an XML document to store some of my information.

I can easily read the values I have pre-written in a well-formed XML document with code like this:

doc = New Xml.XmlDocument
reader = New Xml.XmlTextReader(infoAddress)
doc.Load(reader)

TotPayments = doc.Item("KioskInfo").Item("PayInfo").Item("TotPayments").InnerText


My question is, how do I write back and edit these values Most of the examples I see are only concerned with creating entirely new documents or adding elements. I just want to edit the nodes to hold updated values.

Thanks,
BobSw



Answer this question

How can I edit values in an XML document?

  • Patty B

    How about...

    doc.Item("KioskInfo").Item("PayInfo").Item("TotPayments").InnerText = "500"

    Jason

  • Load

    Sorry I took so long to get back to you, but I got put on another critical project.

    Doc.Save(fileAddress) worked perfectly after I closed the XML Reader.

    Many, many Thanks!

    BobSw


  • damienmorton

    Hi Jason,

    I really appreciate your help!

    'First I'm reading the information

    doc = New Xml.XmlDocument
    reader = New Xml.XmlTextReader(infoAddress)
    doc.Load(reader)

    TotPayments = doc.Item("KioskInfo").Item("PayInfo").Item("TotPayments").InnerText

    'Then I'm doing the calculations

    TotPayments += AmtPaid


    'Then I want to Save the new info

    doc.Item("KioskInfo").Item("PayInfo").Item("TotPayments").InnerText = TotPayments.toString

    reader.close

    'That's about it. I'm reading my values perfectly. I just can't write back again. Am I setting this up all wrong

    Thanks,

    BobSw


  • anilF

    I had to insert the following statement to check the value:

    TotPayments = doc.Item("KioskInfo").Item("PayInfo").Item("TotPayments").InnerText

    It did pull back the updated value, but the underlying document itself was not changed, so with every pass, it's not really getting updated. Is there a save command that I need to run

    Thanks,

    BobSw


  • benney

    You're updating an in-memory copy of the document. If you want to write your changes back to disk, you'll need to call one of the XmlDocument.Save methods.

    Jason


  • aguyngueran

    Could you post some code for me

    Jason


  • jemiller

    Thanks Jason, but it didn't seem to work. The code produced no errors at all when stepping through it. It just didn't change any of the values in my XML document.

    I thought I'd have to use an XmlWriter to do this, but if I don't need it that would be great.

    I converted my values to string with value.toString just to make sure that wasn't an issue.

    Any more thoughts would be appreciated.

    Thanks,

    BobSw


  • Mr. Guglev

    Can you set a breakpoint on the following line...

    doc.Item("KioskInfo").Item("PayInfo").Item("TotPayments").InnerText = TotPayments.toString

    ...and tell me if after you execute it, InnerText contains your new value. It really should.

    Jason


  • How can I edit values in an XML document?