【问题标题】:My Foreach Loop loops through a list of ChildNodes under a Main Node, and only selects the firstNode我的 Foreach 循环遍历主节点下的子节点列表,并且只选择第一个节点
【发布时间】:2018-10-27 14:52:33
【问题描述】:

我正在尝试遍历主节点下的 ChildNode 列表,并且我在 XMLNodeList 中有 ChildNode 列表。我已经成功地循环了它,那部分代码可以工作,但它只会选择 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>

尝试遍历每个事务并将其添加到表中。它只会选择第一个。

【问题讨论】:

  • 使用较新的库 XML Linq 通常会得到更好的结果。您没有发布 xml,所以我真的无能为力。

标签: c# xml foreach selectsinglenode selectnodes


【解决方案1】:

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

            }

        }
    }
}

【讨论】:

  • 我非常感谢您的帮助,由于我一直忙于学校,因此迟到了回复。修改代码需要一点时间,但它奏效了;我只需要在代码末尾运行一个函数,将所有空值更改为 Web 浏览器的可解析字符串。
  • 你指的是整数和小数的空值吗?你想让它们为零吗?或者,也许将值作为字符串而不是数字。
  • 我设法让它工作。必须在 Linq、XElement 中执行一半的循环,而在 c# 中执行另一半。
猜你喜欢
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
  • 2021-11-08
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
相关资源
最近更新 更多