C# 我的Foreach循环遍历主节点下的子节点列表,并且只选择第一个节点
我试图循环浏览主节点下的ChildNodes列表,并且在XMLNodeList中有ChildNodes列表。我已经成功地循环了它,代码的这一部分工作正常,但在使用SelectSingleNode时,它只会选择NodeList的第一个节点。我需要解决这个问题,循环遍历NodeList中的每个XML节点,并从中选择一些子属性。我尝试删除每个节点时都没有成功,我仔细查看了SelectNodes,但它们都有相同的名称;唯一真正的区别是日期。有什么建议吗 代码:C# 我的Foreach循环遍历主节点下的子节点列表,并且只选择第一个节点,c#,xml,foreach,selectsinglenode,selectnodes,C#,Xml,Foreach,Selectsinglenode,Selectnodes,我试图循环浏览主节点下的ChildNodes列表,并且在XMLNodeList中有ChildNodes列表。我已经成功地循环了它,代码的这一部分工作正常,但在使用SelectSingleNode时,它只会选择NodeList的第一个节点。我需要解决这个问题,循环遍历NodeList中的每个XML节点,并从中选择一些子属性。我尝试删除每个节点时都没有成功,我仔细查看了SelectNodes,但它们都有相同的名称;唯一真正的区别是日期。有什么建议吗 代码: XmlNode TransactionMa
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:
尝试循环处理每个事务并将其添加到表中。它将只选择第一个。使用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")
});
}
}
}
}
使用较新的库XML Linq通常会获得更好的结果。你没有发布xml,所以我真的帮不了你。我真的很感谢你的帮助,因为我一直忙着上学,所以回复晚了。修改代码花了一点时间,但效果不错;我只需要在代码末尾运行一个函数,将所有空值更改为web浏览器的可解析字符串。您指的是整数和十进制空值吗?你希望它们为零吗?或者用字符串代替数字,我设法让它工作起来。必须用Linq、XElement和c#完成一半循环。
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")
});
}
}
}
}