Results 1 to 4 of 4

Thread: Reading XML

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2016
    Posts
    21

    Reading XML

    I am trying to create a CRM plugin to read this xml data.


    what I am interested in is looping through the PaymentFile tag to extract the data to variables. i.e get the value of Index and userNumber .. do something with that then get the next index and userNumver values Etc I have cut this example short, but there could be anywhere from 1 to 3000 records in this xml file

    Code:
    <?xml version="1.0" ?>
    <SubmissionResults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" status="complete" submissionIdentifier="1539163533409qFdTb9FGn" submissionType="live" submissionSerialNumber="005928" submissionDateAndTime="Wed Oct 10 10:25:37 BST 2018" submissionEarliestDate="2018-10-11" xmlns="http://bacs.co.uk/submissions">
     <SubmittingServiceUser userNumber="ABCDEF" name="SOME COMPANY" />
     <SubmittingContact contactIdentifier="JOHN123" fullName="JOHN SMITH" />
     <SigningContact contactIdentifier="JOHN123" fullName="JOHN SMITH" />
     <PaymentFile status="complete" index="1" paymentFileIdentifier="499" processingDay="2018-10-11" currency="GBP" creditRecordCount="0" creditValueTotal="0" debitRecordCount="2" debitValueTotal="4200" ddiRecordCount="0" workCode="4 MULTI  ">
      <OriginatingServiceUser userNumber="123456" name="COMPANY 1" />
     </PaymentFile>
     <PaymentFile status="complete" index="2" paymentFileIdentifier="159" processingDay="2018-10-19" currency="GBP" creditRecordCount="0" creditValueTotal="0" debitRecordCount="29" debitValueTotal="665755" ddiRecordCount="0" workCode="4 MULTI  ">
      <OriginatingServiceUser userNumber="121212" name="COMPANY 2" />
     </PaymentFile>
    </SubmissionResults>
    Last edited by dday9; Oct 12th, 2018 at 08:33 AM. Reason: Added Code Tags

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Reading XML

    To actually read the XML into an Object, you can use the XDocument which has the Load or LoadAsync method which can be used to load the XML from a file and then there is the Parse method which can be used to load the an XML literal from a String.

    As a quick example, here is me loading the XML literal you provided in your original post into an XDocument:
    Code:
    Dim literal As String = "<?xml version=""1.0"" ?>
                             <SubmissionResults xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" status=""complete"" submissionIdentifier=""1539163533409qFdTb9FGn"" submissionType=""live"" submissionSerialNumber=""005928"" submissionDateAndTime=""Wed Oct 10 10:25:37 BST 2018"" submissionEarliestDate=""2018-10-11"" xmlns=""http://bacs.co.uk/submissions"">
                                <SubmittingServiceUser userNumber=""ABCDEF"" name=""SOME COMPANY"" />
                                <SubmittingContact contactIdentifier=""JOHN123"" fullName=""JOHN SMITH"" />
                                <SigningContact contactIdentifier=""JOHN123"" fullName=""JOHN SMITH"" />
                                <PaymentFile status=""complete"" index=""1"" paymentFileIdentifier=""499"" processingDay=""2018-10-11"" currency=""GBP"" creditRecordCount=""0"" creditValueTotal=""0"" debitRecordCount=""2"" debitValueTotal=""4200"" ddiRecordCount=""0"" workCode=""4 MULTI  "">
                                   <OriginatingServiceUser userNumber=""123456"" name=""COMPANY 1"" />
                                </PaymentFile>
                                <PaymentFile status=""complete"" index=""2"" paymentFileIdentifier=""159"" processingDay=""2018-10-19"" currency=""GBP"" creditRecordCount=""0"" creditValueTotal=""0"" debitRecordCount=""29"" debitValueTotal=""665755"" ddiRecordCount=""0"" workCode=""4 MULTI  "">
                                   <OriginatingServiceUser userNumber=""121212"" name=""COMPANY 2"" />
                                </PaymentFile>
                             </SubmissionResults>"
    
    Dim document As XDocument = XDocument.Parse(literal)
    As far as getting specific values in the XDocument, you would get the XElements via the Elements method. So in your case it would look like the following:
    Code:
    Dim originatingServiceUser As XElement
    Dim index, userNumber As XAttribute
    'Iterate through each PaymentFile XElement in the document
    For Each paymentFile As XElement In document.Root.Elements("PaymentFile")
        'Get the index attribute
        index = paymentFile.Attribute("index")
    
        'Get the first OriginatingServiceUser element from the currently iterated PaymentFile element
        originatingServiceUser = paymentFile.Element("OriginatingServiceUser")
    
        'Check if there was an OriginatingServiceUser element
        If originatingServiceUser IsNot Nothing Then
            'Get the userNumber
            userNumber = originatingServiceUser.Attribute("userNumber")
        End If
    
        'Print the values
        Console.WriteLine("Index: {0} UserNumber: {1}", index.Value, userNumber.Value)
    
        'Reset the values
        originatingServiceUser = Nothing : index = Nothing : userNumber = Nothing
    Next
    
    Console.ReadLine()
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Reading XML

    Another way might be to load it into a DataTable/DataSet using the ReadXml method.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2016
    Posts
    21

    Re: Reading XML

    That's perfect, Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width