Christopher Hanka
Christopher Hanka

Reputation: 13

My Foreach Loop loops through a list of ChildNodes under a Main Node, and only selects the firstNode

I'm trying to loop through a list of ChildNodes under a main node, and I have the list of ChildNodes in a XMLNodeList. I've successfully looped through it, and that part of the code works, but it will only select the firstnode of the NodeList as I'm using SelectSingleNode. I need a work-around for this problem, to loop through every single XML node in the NodeList and select some child attributes from it. I've unsuccessfully tried to delete each node as I'm going through it, I've looked into SelectNodes a little bit, but they all have the same name; the only real difference is the date. Any suggestions?

Code:

XmlNode TransactionMain = XMLFromServer.SelectSingleNode("");
XmlNodeList TransactionsNodeList = TransactionMain.ChildNodes;
foreach(XmlNode TransactionsSingleNode in TransactionsNodeList) {
    //Finding all of the pertinent information for the Transactions table from EACH node (note the foreach loop) inside the bigger Transaction node
    string SalesTaxAmount = TransactionsSingleNode.SelectNodes("").Value;
    string PaymentAmount = TransactionsSingleNode.SelectSingleNode("").Value;
    string RecurringProfileID = TransactionsSingleNode.SelectSingleNode("").Value;
    string TransactedDateTime = TransactionsSingleNode.SelectSingleNode("").Value;
    string NumberOfDays = TransactionsSingleNode.SelectSingleNode("").Value;
    string LicenseSizeDescriptionTransactionsList = TransactionsSingleNode.SelectSingleNode("").Value;
    Transactions.Rows.Add(CompanyName, SalesTaxAmount, PaymentAmount, RecurringProfileID, TransactedDateTime, NumberOfDays, LicenseSizeDescriptionTransactionsList);

The CompanyName variable comes from another place, that works fine. e I've removed the XPath expressions, as it contained a bit of private information. If it's necessary I can put them back in. XML:

<Authorization LicenseDaysLeft="22" LicenseEndDate="2018-11-17" LicenseSizeDescription="" LicenseSizeCode="1">
  <Paypal LastEmailUsed="" RecurringProfileID="" LastTransactionDateTime="">
     <Transactions>
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-10-18 07:54" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-09-18 08:19" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-08-19 00:38" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-07-20 09:16" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-06-20 01:24" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-05-21 09:06" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-04-19 08:31" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-03-13 08:58" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-02-11 08:22" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2018-01-17 10:22" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-12-13 10:08" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-11-13 12:04" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-10-14 10:18" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-09-20 07:14" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-08-22 23:52" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-07-21 08:36" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-06-24 08:58" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-05-24 11:24" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-04-17 16:42" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-03-18 09:02" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-02-16 15:08" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2017-01-23 09:13" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2016-12-24 18:07" NumberOfDays="30" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2016-11-23 16:09" NumberOfDays="0" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2016-10-24 14:37" NumberOfDays="0" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2016-09-24 15:58" NumberOfDays="0" LicenseSize="0" LicenseSizeDescription="" />
        <Transaction SalesTaxAmount="0.00" PaymentAmount="0.00" RecurringProfileID="" TransactedDateTimeUTC="0001-01-01 00:00" TransactedDateTime="2016-08-25 16:48" NumberOfDays="0" LicenseSize="0" LicenseSizeDescription="" />
     </Transactions>
  </Paypal>
 </Authorization>

Trying to loop through each Transaction and add it to a table. It will only select the first one.

Upvotes: -1

Views: 97

Answers (1)

jdweng
jdweng

Reputation: 34421

It is very simple with xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("SalesTaxAmount", typeof(decimal));
            dt.Columns.Add("PaymentAmount", typeof(decimal)); 
            dt.Columns.Add("RecurringProfileID", typeof(string)); 
            dt.Columns.Add("TransactedDateTimeUTC", typeof(DateTime)); 
            dt.Columns.Add("TransactedDateTime", typeof(DateTime)); 
            dt.Columns.Add("NumberOfDays", typeof(int)); 
            dt.Columns.Add("LicenseSize", typeof(int));
            dt.Columns.Add("LicenseSizeDescription=", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);
            foreach(XElement transaction in doc.Descendants("Transaction"))
            {
                dt.Rows.Add(new object[] {
                    (decimal?)transaction.Attribute("SalesTaxAmount"),
                    (decimal?)transaction.Attribute("PaymentAmount"),
                    (string)transaction.Attribute("RecurringProfileID"),
                    (DateTime)transaction.Attribute("TransactedDateTimeUTC"),
                    (DateTime)transaction.Attribute("TransactedDateTime"),
                    (int?)transaction.Attribute("NumberOfDays"),
                    (int)transaction.Attribute("LicenseSize"),
                    (string)transaction.Attribute("LicenseSizeDescription")
                });

            }

        }
    }
}

Upvotes: -1

Related Questions