Мой цикл по каждому элементу просматривает список дочерних узлов под основным узлом и выбирает только первый узел - PullRequest
0 голосов
/ 27 октября 2018

Я пытаюсь перебрать список дочерних узлов под основным узлом, и у меня есть список дочерних узлов в XMLNodeList.Я успешно прошел через это, и эта часть кода работает, но он выберет только первый узел NodeList, так как я использую SelectSingleNode.Мне нужен обходной путь для этой проблемы, чтобы пройти через каждый отдельный узел XML в NodeList и выбрать из него некоторые дочерние атрибуты.Я безуспешно пытался удалить каждый узел при прохождении через него, я немного изучил SelectNodes, но все они имеют одинаковые имена;единственная реальная разница - это дата.Любые предложения?

Код:

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);

Переменная CompanyName происходит из другого места, которая работает нормально.e Я удалил выражения XPath, так как они содержали частную информацию.При необходимости я могу вернуть их обратно. 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>

Попытка перебрать каждую транзакцию и добавить ее в таблицу.Он выберет только первый.

1 Ответ

0 голосов
/ 28 октября 2018

Это очень просто с 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")
                });

            }

        }
    }
}
...