Home > OS >  How do I get a particular InnerText in this XML file?
How do I get a particular InnerText in this XML file?

Time:02-10

I need to parse out the Amount node inside the first TotalNetChargeWithDutiesAndTaxes parent node in the sample XML below. How do I do this? I've tried a bunch of stuff but I can't figure out a way to specifically get the value by name of nodes.

The code below is what I have so far, but this is a really hacky way of doing this. I don't know if I can rely on the Amount node always being the second element of the TotalNetChargeWithDutiesAndTaxes node, so I need a way to search this XML doc specifically by the names of the nodes.

Dim elemList as XmlNodeList = Doc.GetElementsByTagName("TotalNetChargeWithDutiesAndTaxes")
                
MsgBox(elemList(0).ChildNodes(1).InnerText)

The full XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <RateReply xmlns="http://fedex.com/ws/rate/v31">
         <HighestSeverity>SUCCESS</HighestSeverity>
         <Notifications>
            <Severity>SUCCESS</Severity>
            <Source>crs</Source>
            <Code>0</Code>
            <Message>Request was successfully processed. </Message>
            <LocalizedMessage>Request was successfully processed. </LocalizedMessage>
         </Notifications>
         <TransactionDetail>
            <CustomerTransactionId>94c345b5-55d6-4a44-9c97-d5809179eaa4</CustomerTransactionId>
         </TransactionDetail>
         <Version>
            <ServiceId>crs</ServiceId>
            <Major>31</Major>
            <Intermediate>0</Intermediate>
            <Minor>0</Minor>
         </Version>
         <RateReplyDetails>
            <ServiceType>FEDEX_GROUND</ServiceType>
            <ServiceDescription>
               <ServiceType>FEDEX_GROUND</ServiceType>
               <Code>92</Code>
               <Names>
                  <Type>long</Type>
                  <Encoding>utf-8</Encoding>
                  <Value>FedEx Ground®</Value>
               </Names>
               <Names>
                  <Type>long</Type>
                  <Encoding>ascii</Encoding>
                  <Value>FedEx Ground</Value>
               </Names>
               <Names>
                  <Type>medium</Type>
                  <Encoding>utf-8</Encoding>
                  <Value>Ground®</Value>
               </Names>
               <Names>
                  <Type>medium</Type>
                  <Encoding>ascii</Encoding>
                  <Value>Ground</Value>
               </Names>
               <Names>
                  <Type>short</Type>
                  <Encoding>utf-8</Encoding>
                  <Value>FG</Value>
               </Names>
               <Names>
                  <Type>short</Type>
                  <Encoding>ascii</Encoding>
                  <Value>FG</Value>
               </Names>
               <Names>
                  <Type>abbrv</Type>
                  <Encoding>ascii</Encoding>
                  <Value>SG</Value>
               </Names>
               <Description>FedEx Ground</Description>
               <AstraDescription>FXG</AstraDescription>
            </ServiceDescription>
            <PackagingType>YOUR_PACKAGING</PackagingType>
            <DestinationAirportId>ORD</DestinationAirportId>
            <IneligibleForMoneyBackGuarantee>false</IneligibleForMoneyBackGuarantee>
            <SignatureOption>SERVICE_DEFAULT</SignatureOption>
            <ActualRateType>PAYOR_ACCOUNT_PACKAGE</ActualRateType>
            <RatedShipmentDetails>
               <EffectiveNetDiscount>
                  <Currency>USD</Currency>
                  <Amount>0.0</Amount>
               </EffectiveNetDiscount>
               <ShipmentRateDetail>
                  <RateType>PAYOR_ACCOUNT_PACKAGE</RateType>
                  <RateZone>5</RateZone>
                  <RatedWeightMethod>DIM</RatedWeightMethod>
                  <DimDivisor>139</DimDivisor>
                  <FuelSurchargePercent>11.75</FuelSurchargePercent>
                  <TotalBillingWeight>
                     <Units>LB</Units>
                     <Value>13.0</Value>
                  </TotalBillingWeight>
                  <TotalDimWeight>
                     <Units>LB</Units>
                     <Value>13.0</Value>
                  </TotalDimWeight>
                  <TotalBaseCharge>
                     <Currency>USD</Currency>
                     <Amount>16.87</Amount>
                  </TotalBaseCharge>
                  <TotalFreightDiscounts>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalFreightDiscounts>
                  <TotalNetFreight>
                     <Currency>USD</Currency>
                     <Amount>16.87</Amount>
                  </TotalNetFreight>
                  <TotalSurcharges>
                     <Currency>USD</Currency>
                     <Amount>1.98</Amount>
                  </TotalSurcharges>
                  <TotalNetFedExCharge>
                     <Currency>USD</Currency>
                     <Amount>18.85</Amount>
                  </TotalNetFedExCharge>
                  <TotalTaxes>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalTaxes>
                  <TotalNetCharge>
                     <Currency>USD</Currency>
                     <Amount>18.85</Amount>
                  </TotalNetCharge>
                  <TotalRebates>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalRebates>
                  <TotalDutiesAndTaxes>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalDutiesAndTaxes>
                  <TotalAncillaryFeesAndTaxes>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalAncillaryFeesAndTaxes>
                  <TotalDutiesTaxesAndFees>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalDutiesTaxesAndFees>
                  <TotalNetChargeWithDutiesAndTaxes>
                     <Currency>USD</Currency>
                     <Amount>18.85</Amount>
                  </TotalNetChargeWithDutiesAndTaxes>
                  <Surcharges>
                     <SurchargeType>FUEL</SurchargeType>
                     <Level>PACKAGE</Level>
                     <Description>FedEx Ground Fuel</Description>
                     <Amount>
                        <Currency>USD</Currency>
                        <Amount>1.98</Amount>
                     </Amount>
                  </Surcharges>
               </ShipmentRateDetail>
               <RatedPackages>
                  <GroupNumber>1</GroupNumber>
                  <EffectiveNetDiscount>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </EffectiveNetDiscount>
                  <PackageRateDetail>
                     <RateType>PAYOR_ACCOUNT_PACKAGE</RateType>
                     <RatedWeightMethod>DIM</RatedWeightMethod>
                     <BillingWeight>
                        <Units>LB</Units>
                        <Value>13.0</Value>
                     </BillingWeight>
                     <DimWeight>
                        <Units>LB</Units>
                        <Value>13.0</Value>
                     </DimWeight>
                     <BaseCharge>
                        <Currency>USD</Currency>
                        <Amount>16.87</Amount>
                     </BaseCharge>
                     <TotalFreightDiscounts>
                        <Currency>USD</Currency>
                        <Amount>0.0</Amount>
                     </TotalFreightDiscounts>
                     <NetFreight>
                        <Currency>USD</Currency>
                        <Amount>16.87</Amount>
                     </NetFreight>
                     <TotalSurcharges>
                        <Currency>USD</Currency>
                        <Amount>1.98</Amount>
                     </TotalSurcharges>
                     <NetFedExCharge>
                        <Currency>USD</Currency>
                        <Amount>18.85</Amount>
                     </NetFedExCharge>
                     <TotalTaxes>
                        <Currency>USD</Currency>
                        <Amount>0.0</Amount>
                     </TotalTaxes>
                     <NetCharge>
                        <Currency>USD</Currency>
                        <Amount>18.85</Amount>
                     </NetCharge>
                     <TotalRebates>
                        <Currency>USD</Currency>
                        <Amount>0.0</Amount>
                     </TotalRebates>
                     <Surcharges>
                        <SurchargeType>FUEL</SurchargeType>
                        <Level>PACKAGE</Level>
                        <Description>FedEx Ground Fuel</Description>
                        <Amount>
                           <Currency>USD</Currency>
                           <Amount>1.98</Amount>
                        </Amount>
                     </Surcharges>
                  </PackageRateDetail>
               </RatedPackages>
            </RatedShipmentDetails>
            <RatedShipmentDetails>
               <ShipmentRateDetail>
                  <RateType>PAYOR_LIST_PACKAGE</RateType>
                  <RateZone>5</RateZone>
                  <RatedWeightMethod>DIM</RatedWeightMethod>
                  <DimDivisor>139</DimDivisor>
                  <FuelSurchargePercent>11.75</FuelSurchargePercent>
                  <TotalBillingWeight>
                     <Units>LB</Units>
                     <Value>13.0</Value>
                  </TotalBillingWeight>
                  <TotalDimWeight>
                     <Units>LB</Units>
                     <Value>13.0</Value>
                  </TotalDimWeight>
                  <TotalBaseCharge>
                     <Currency>USD</Currency>
                     <Amount>16.87</Amount>
                  </TotalBaseCharge>
                  <TotalFreightDiscounts>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalFreightDiscounts>
                  <TotalNetFreight>
                     <Currency>USD</Currency>
                     <Amount>16.87</Amount>
                  </TotalNetFreight>
                  <TotalSurcharges>
                     <Currency>USD</Currency>
                     <Amount>1.98</Amount>
                  </TotalSurcharges>
                  <TotalNetFedExCharge>
                     <Currency>USD</Currency>
                     <Amount>18.85</Amount>
                  </TotalNetFedExCharge>
                  <TotalTaxes>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalTaxes>
                  <TotalNetCharge>
                     <Currency>USD</Currency>
                     <Amount>18.85</Amount>
                  </TotalNetCharge>
                  <TotalRebates>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalRebates>
                  <TotalDutiesAndTaxes>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalDutiesAndTaxes>
                  <TotalAncillaryFeesAndTaxes>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalAncillaryFeesAndTaxes>
                  <TotalDutiesTaxesAndFees>
                     <Currency>USD</Currency>
                     <Amount>0.0</Amount>
                  </TotalDutiesTaxesAndFees>
                  <TotalNetChargeWithDutiesAndTaxes>
                     <Currency>USD</Currency>
                     <Amount>18.85</Amount>
                  </TotalNetChargeWithDutiesAndTaxes>
                  <Surcharges>
                     <SurchargeType>FUEL</SurchargeType>
                     <Level>PACKAGE</Level>
                     <Description>FedEx Ground Fuel</Description>
                     <Amount>
                        <Currency>USD</Currency>
                        <Amount>1.98</Amount>
                     </Amount>
                  </Surcharges>
               </ShipmentRateDetail>
               <RatedPackages>
                  <GroupNumber>1</GroupNumber>
                  <PackageRateDetail>
                     <RateType>PAYOR_LIST_PACKAGE</RateType>
                     <RatedWeightMethod>DIM</RatedWeightMethod>
                     <BillingWeight>
                        <Units>LB</Units>
                        <Value>13.0</Value>
                     </BillingWeight>
                     <DimWeight>
                        <Units>LB</Units>
                        <Value>13.0</Value>
                     </DimWeight>
                     <BaseCharge>
                        <Currency>USD</Currency>
                        <Amount>16.87</Amount>
                     </BaseCharge>
                     <TotalFreightDiscounts>
                        <Currency>USD</Currency>
                        <Amount>0.0</Amount>
                     </TotalFreightDiscounts>
                     <NetFreight>
                        <Currency>USD</Currency>
                        <Amount>16.87</Amount>
                     </NetFreight>
                     <TotalSurcharges>
                        <Currency>USD</Currency>
                        <Amount>1.98</Amount>
                     </TotalSurcharges>
                     <NetFedExCharge>
                        <Currency>USD</Currency>
                        <Amount>18.85</Amount>
                     </NetFedExCharge>
                     <TotalTaxes>
                        <Currency>USD</Currency>
                        <Amount>0.0</Amount>
                     </TotalTaxes>
                     <NetCharge>
                        <Currency>USD</Currency>
                        <Amount>18.85</Amount>
                     </NetCharge>
                     <TotalRebates>
                        <Currency>USD</Currency>
                        <Amount>0.0</Amount>
                     </TotalRebates>
                     <Surcharges>
                        <SurchargeType>FUEL</SurchargeType>
                        <Level>PACKAGE</Level>
                        <Description>FedEx Ground Fuel</Description>
                        <Amount>
                           <Currency>USD</Currency>
                           <Amount>1.98</Amount>
                        </Amount>
                     </Surcharges>
                  </PackageRateDetail>
               </RatedPackages>
            </RatedShipmentDetails>
         </RateReplyDetails>
      </RateReply>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

CodePudding user response:

You can use the XmlDocument to load the xml and iterate through node to get its value. See below example

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(xml);
    XmlNode totalNet = xmlDocument.SelectSingleNode("//*[local-name()='TotalNetChargeWithDutiesAndTaxes']");
    if (totalNet.ChildNodes.Count > 0)
    {
        foreach (XmlNode i in totalNet)
        {
            if (i.InnerText != null && i.Name=='Amount')
            {
                amtValue=i.InnerText
            }
        }
    }

CodePudding user response:

Like this:

Public Sub test()
    Dim xDoc, nodes, oNode
    
    Set xDoc = CreateObject("MSXML2.DOMDocument.6.0")
    xDoc.setProperty "SelectionLanguage", "XPath"
    
    'Note: added an `x=` to the default namespace so we can reference it later
    xDoc.setProperty "SelectionNamespaces", _
                     "xmlns:x='http://fedex.com/ws/rate/v31'"
    
    xDoc.LoadXML GetContent("C:\temp\tmp.xml") 'load XML from file
    
    Set nodes = xDoc.SelectNodes("//x:TotalNetChargeWithDutiesAndTaxes/x:Amount")
    Debug.Print "Found: " & nodes.Length
    
    For Each oNode In nodes
        Debug.Print oNode.nodeTypedValue
    Next oNode
End Sub

'read all content from a text file
Function GetContent(f As String) As String
    GetContent = CreateObject("scripting.filesystemobject"). _
                  opentextfile(f, 1).readall()
End Function
  • Related