Solved

Editing Node of XML Document

Posted on 2006-06-27
3
224 Views
Last Modified: 2010-08-05
Hello,
I have an XML Document that looks like the following. I am trying to find out how to add a value to the nodes below. For instance If I want to add 1999-01-01 to From Report Date and 1999-02-02 to To Report Date.

 <FromReportDate> </FromReportDate> and <ToReportDate></ToReportDate>

My 2nd question is, how to Replace and add a value if something is exsistent there. Lets says 1999-01-01 is in FromReportDate, I want it to replace 1999-01-01 and add whatever the new value is, instead of just adding to the exsisting value)
Something like

If  Value at FromReportDate
Then clear and add with textbox1.text
If no value at FromReportDate
then Add with textboxt1.text

Thanks!



<?xml version="1.0" ?>
<?qbxml version="5.0"?>
<QBXML>
<QBXMLMsgsRq onError = "continueOnError">
  <PayrollSummaryReportQueryRq>
    <PayrollSummaryReportType>PayrollSummary</PayrollSummaryReportType>
    <ReportPeriod>
      <FromReportDate></FromReportDate>
      <ToReportDate></ToReportDate>
      </ReportPeriod>
      

  </PayrollSummaryReportQueryRq>


 </QBXMLMsgsRq>
</QBXML>
0
Comment
Question by:JasonWinn
  • 2
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16995218
Assuming you have loaded your XML into a DOMDocument called oDom:

    Set oNode = oDom.selectSingleNode("//FromReportDate")
    If Not oNode Is Nothing Then
        oNode.Text = "1999-01-01"                           '// Replace existing text
        oNode.Text = oNode.Text & "1999-01-01"      '// Append to existing text
    End If
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 16995285
Hey Carl_tawn,
Thanks for the quick reply. I am pretty new to VB so I am not quit sure how to load the DOMDocument.

Right now I just have   this:

        Dim xDoc As XmlDocument

        xDoc = New XmlDocument
        xDoc.Load("C:\Documents and Settings\payrollSummaryPrevious.xml")







        xDoc.Save("C:\Documents and Settings\payrollSummaryPrevious.xml")
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 16995325
Ah, you're using VB.Net rather than VB6.

What you have already creates an XmlDocument object and loads your XML file from disc. So, to modify the text of a node you would add something like:

        xDoc = New XmlDocument
        xDoc.Load("C:\Documents and Settings\payrollSummaryPrevious.xml")

        Dim node As XmlNode = xDoc.SelectSingleNode("//FromReportDate")
       
        node.InnerText = "1999-01-01"        '// Change the current text
        node.InnerText &= "1999-01-01"      '// Append to the current text

        xDoc.Save("C:\Documents and Settings\payrollSummaryPrevious.xml")
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question