xmlvb.netlinq-to-xml

modify XML Element using Xdocument


I am trying to modify the date for the FromModifiedDate line in the xml file i would first like to show the date before changing but i dont know how to traverse this xml file to get to the FromModifiedDate the console.writeline within the for next loop never gets hit

here is my code so far

 Dim document1 As XDocument = XDocument.Load("C:\QuickBooks Shared\SDKTestPlus3\BillQuery.xml")

    '  For Each ModifiedDateRangeFilter In From element In document1.<QBXML>.<QBXMLMsgsRq>.<BillQueryRq>.<ModifiedDateRangeFilter>
    'ModifiedDateRangeFilter.<FromModifiedDate>.value = "2021-01-01"
    '  Next
    For Each daterange As XElement In document1.Descendants("ModifiedDateRangeFilter")

        Console.WriteLine(daterange.Elements("FromModifiedDate"))
    Next


    Console.WriteLine(document1.Root.Element("QBXMLMsgsRq").<BillQueryRq>.Value)
    '.Elements("ModifiedDateRangeFilter").Elements("FromModifiedDate")

the second writeline outputs 2021-01-012023-12-311TxnIDTimeCreatedTimeModifiedVendorRefVendorAddressAPAccountRefTxnDateDueDateAmountDueIsPendingTermsRefIsPaidOpenAmount

and here is my XML file

    <?xml version="1.0" encoding="utf-8"?>
<?qbxml version="16.0"?>
<QBXML>
    <QBXMLMsgsRq onError="stopOnError">
        <BillQueryRq>
            <ModifiedDateRangeFilter> <!-- optional -->
                <FromModifiedDate>2021-01-01</FromModifiedDate> <!-- optional -->
                <ToModifiedDate>2023-12-31</ToModifiedDate> <!-- optional -->
            </ModifiedDateRangeFilter>
            <IncludeLineItems >1</IncludeLineItems> <!-- optional -->
            <IncludeRetElement >TxnID</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >TimeCreated</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >TimeModified</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >VendorRef</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >VendorAddress</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >APAccountRef</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >TxnDate</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >DueDate</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >AmountDue</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >IsPending</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >TermsRef</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >IsPaid</IncludeRetElement> <!-- optional, may repeat -->
            <IncludeRetElement >OpenAmount</IncludeRetElement> <!-- optional, may repeat -->
        </BillQueryRq>
    </QBXMLMsgsRq>
</QBXML>

Solution

  • Please try the following solution leveraging LINQ to XML API functionality.

    No need in any loops.

    VB.NET

    Sub Main
        Const FILENAME As String = "e:\Temp\BillQuery.xml"
        Dim FromModifiedDate_New As String = "2024-05-16"
    
        Dim xdoc As XDocument = XDocument.Load(FILENAME)
        Dim FromModifiedDate = xdoc.Descendants("FromModifiedDate")?.SingleOrDefault()?.Value
    
        If Not String.IsNullOrEmpty(FromModifiedDate) Then
            Console.WriteLine(FromModifiedDate)
            Dim xelem As XElement = xdoc.Descendants("FromModifiedDate").SingleOrDefault()
            xelem.SetValue(FromModifiedDate_New)
        End If
    End Sub
    

    Output

    2021-01-01
    
    <?qbxml version="16.0"?>
    <QBXML>
      <QBXMLMsgsRq onError="stopOnError">
        <BillQueryRq>
          <ModifiedDateRangeFilter>
            <!-- optional -->
            <FromModifiedDate>2024-05-16</FromModifiedDate>
            <!-- optional -->
            <ToModifiedDate>2023-12-31</ToModifiedDate>
            <!-- optional -->
          </ModifiedDateRangeFilter>
          <IncludeLineItems>1</IncludeLineItems>
          <!-- optional -->
          <IncludeRetElement>TxnID</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>TimeCreated</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>TimeModified</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>VendorRef</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>VendorAddress</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>APAccountRef</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>TxnDate</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>DueDate</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>AmountDue</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>IsPending</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>TermsRef</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>IsPaid</IncludeRetElement>
          <!-- optional, may repeat -->
          <IncludeRetElement>OpenAmount</IncludeRetElement>
          <!-- optional, may repeat -->
        </BillQueryRq>
      </QBXMLMsgsRq>
    </QBXML>