我正在尝试遍历主节点下的 ChildNodes 列表,并且我在 XMLNodeList 中有 ChildNodes 列表。我已经成功地循环了它,那部分代码可以工作,但它只会选择 NodeList 的第一个节点,因为我正在使用 SelectSingleNode。我需要解决这个问题,循环遍历 NodeList 中的每个 XML 节点并从中选择一些子属性。我在浏览每个节点时尝试删除它,但没有成功,我稍微研究了 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>
尝试遍历每个事务并将其添加到表中。它只会选择第一个。