如果LinkedTxn
为RefNumber
,我尝试使用C#作为DataTable来阅读以下XML以获取TxnType
,BillPaymentCheck
。 LinkedTxn
某个时间也可能会<TxnType>PurchaseOrder</TxnType>
,但我只想获得<TxnType>BillPaymentCheck</TxnType>
。对于BillRet
级别,这可能是2/3次。
如果有2/3 LinkedTxn
个部分,则需要在数据表中有2/3个数据行。
<?xml version="1.0" ?>
<QBXML>
<QBXMLMsgsRs>
<BillQueryRs requestID="0" statusCode="0" statusSeverity="Info" statusMessage="Status OK">
<BillRet>
<TxnID>3F9C-1071507206</TxnID>
<TimeCreated>2003-12-15T22:23:26+05:30</TimeCreated>
<TimeModified>2016-12-15T14:18:52+05:30</TimeModified>
<EditSequence>1481791723</EditSequence>
<TxnNumber>857</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-06-13</TxnDate>
<DueDate>2018-07-13</DueDate>
<AmountDue>1214.89</AmountDue>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>42A2-1071524160</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-08-08</TxnDate>
<RefNumber>371</RefNumber>
<LinkType>AMTTYPE</LinkType>
<Amount>-1214.89</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
<BillRet>
<TxnID>3F36-1071505009</TxnID>
<TimeCreated>2003-12-15T21:46:49+05:30</TimeCreated>
<TimeModified>2016-12-15T14:18:51+05:30</TimeModified>
<EditSequence>1481791721</EditSequence>
<TxnNumber>847</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-06-15</TxnDate>
<DueDate>2018-07-15</DueDate>
<AmountDue>1109.15</AmountDue>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>429D-1071524130</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-06-25</TxnDate>
<RefNumber>338</RefNumber>
<LinkType>AMTTYPE</LinkType>
<Amount>-1109.15</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
<BillRet>
<TxnID>325C-1071513076</TxnID>
<TimeCreated>2003-12-16T00:01:16+05:30</TimeCreated>
<TimeModified>2016-12-15T14:17:53+05:30</TimeModified>
<EditSequence>1481791672</EditSequence>
<TxnNumber>547</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-07-30</TxnDate>
<DueDate>2018-08-29</DueDate>
<AmountDue>754.50</AmountDue>
<RefNumber>35698</RefNumber>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>3258-1071513042</TxnID>
<TxnType>PurchaseOrder</TxnType>
<TxnDate>2017-07-23</TxnDate>
<RefNumber>6207</RefNumber>
<LinkType>QUANTYPE</LinkType>
<Amount>0.00</Amount>
</LinkedTxn>
<LinkedTxn>
<TxnID>326A-1071513156</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-12-15</TxnDate>
<RefNumber>503</RefNumber>
<LinkType>AMTTYPE</LinkType>
<Amount>-754.50</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
<BillRet>
<TxnID>3F33-1071504970</TxnID>
<TimeCreated>2003-12-15T21:46:10+05:30</TimeCreated>
<TimeModified>2016-12-15T14:18:44+05:30</TimeModified>
<EditSequence>1481791721</EditSequence>
<TxnNumber>846</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-11-01</TxnDate>
<DueDate>2018-12-01</DueDate>
<AmountDue>656.23</AmountDue>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>3FB5-1071507296</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-11-30</TxnDate>
<RefNumber>472</RefNumber>
<LinkType>AMTTYPE</LinkType>
<Amount>-656.23</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
<BillRet>
<TxnID>A16-933373605</TxnID>
<TimeCreated>1999-07-31T03:56:45+05:30</TimeCreated>
<TimeModified>2016-12-15T14:18:05+05:30</TimeModified>
<EditSequence>1481791619</EditSequence>
<TxnNumber>191</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-11-17</TxnDate>
<DueDate>2018-12-17</DueDate>
<AmountDue>325.00</AmountDue>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>36AD-1071520917</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-12-05</TxnDate>
<RefNumber>484</RefNumber>
<LinkType>AMTTYPE</LinkType>
<Amount>-325.00</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
<BillRet>
<TxnID>40C1-1071514252</TxnID>
<TimeCreated>2003-12-16T00:20:52+05:30</TimeCreated>
<TimeModified>2016-12-15T14:18:51+05:30</TimeModified>
<EditSequence>1481791728</EditSequence>
<TxnNumber>891</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-12-01</TxnDate>
<DueDate>2018-12-31</DueDate>
<AmountDue>400.00</AmountDue>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>428C-1071523939</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-12-15</TxnDate>
<RefNumber>512</RefNumber>
<LinkType>AMTTYPE</LinkType>
<Amount>-400.00</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
<BillRet>
<TxnID>863D-1197708527</TxnID>
<TimeCreated>2007-12-15T14:18:47+05:30</TimeCreated>
<TimeModified>2017-12-15T22:44:33+05:30</TimeModified>
<EditSequence>1481791803</EditSequence>
<TxnNumber>1415</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-12-01</TxnDate>
<DueDate>2018-12-31</DueDate>
<AmountDue>210.00</AmountDue>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>1B953-1513358073</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-12-15</TxnDate>
<LinkType>AMTTYPE</LinkType>
<Amount>-210.00</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
<BillRet>
<TxnID>F34-933787718</TxnID>
<TimeCreated>1999-08-04T22:58:38+05:30</TimeCreated>
<TimeModified>2017-12-15T23:26:55+05:30</TimeModified>
<EditSequence>1481791630</EditSequence>
<TxnNumber>290</TxnNumber>
<VendorRef>
<ListID>B0000-933272656</ListID>
<FullName>Patton Hardware Supplies</FullName>
</VendorRef>
<APAccountRef>
<ListID>C0000-933270541</ListID>
<FullName>Accounts Payable</FullName>
</APAccountRef>
<TxnDate>2017-12-12</TxnDate>
<DueDate>2019-01-11</DueDate>
<AmountDue>810.00</AmountDue>
<TermsRef>
<ListID>10000-933272658</ListID>
<FullName>Net 30</FullName>
</TermsRef>
<IsPaid>true</IsPaid>
<LinkedTxn>
<TxnID>1BD52-1513360613</TxnID>
<TxnType>BillPaymentCheck</TxnType>
<TxnDate>2017-12-15</TxnDate>
<LinkType>AMTTYPE</LinkType>
<Amount>-810.00</Amount>
</LinkedTxn>
<OpenAmount>3459.20</OpenAmount>
</BillRet>
</BillQueryRs>
</QBXMLMsgsRs>
</QBXML>
编码如下,
foreach (XmlNode chlnode in invoiceElement.ChildNodes)
{
if (chlnode.Name == "LinkedTxn")
{
if (Convert.ToString(chlnode["TxnType"].InnerText) == "BillPaymentCheck")
{
//RefNumber
if (chlnode["RefNumber"] != null)
{
d["CheckNum"] = Convert.ToString(chlnode["RefNumber"].InnerText);
if (count == 0)
{
dtTemp.Rows.Add(d);
}
else
{
d = dtTemp.NewRow();
d["CheckNum"] = Convert.ToString(chlnode["RefNumber"].InnerText);
dtTemp.Rows.Add(d);
}
}
}
else
{
//dtTemp.Rows.Add(d);
}
}
}
但是没有工作,为所有记录返回相同的行。
答案 0 :(得分:1)
看起来你不知道XPath。
// all linked transactions that are bill payment checks and have a RefNumber
var xpath = "//BillRet/LinkedTxn[TxnType = 'BillPaymentCheck' and RefNumber > 0]";
foreach (var linkedTxn in invoiceElement.SelectNodes(xpath)) {
var d = dtTemp.NewRow();
d["CheckNum"] = linkedTxn["RefNumber"].InnerText;
dtTemp.Rows.Add(d)
}
请参阅XmlNode.SelectNodes
Method。阅读XPath将为您节省大量时间和打字。
答案 1 :(得分:0)
尝试下面的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 ConsoleApplication2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("TxnID",typeof(string));
dt.Columns.Add("TimeCreated",typeof(DateTime));
dt.Columns.Add("TimeModified",typeof(DateTime));
dt.Columns.Add("EditSequence",typeof(long));
dt.Columns.Add("TxnNumber",typeof(int));
dt.Columns.Add("VendorListID",typeof(string));
dt.Columns.Add("VendorFullName",typeof(string));
dt.Columns.Add("APAccountListID", typeof(string));
dt.Columns.Add("APAccountFullName", typeof(string));
dt.Columns.Add("TxnDate", typeof(DateTime));
dt.Columns.Add("DueDate",typeof(DateTime));
dt.Columns.Add("AmountDue",typeof(decimal));
dt.Columns.Add("TermsListID",typeof(string));
dt.Columns.Add("TermsFullName",typeof(string));
dt.Columns.Add("IsPaid",typeof(Boolean));
dt.Columns.Add("LinkedTxnID", typeof(string));
dt.Columns.Add("LinkedTxnType", typeof(string));
dt.Columns.Add("LinkedTxnDate", typeof(DateTime));
dt.Columns.Add("LinkedRefNumber", typeof(int));
dt.Columns["LinkedRefNumber"].AllowDBNull = true;
dt.Columns.Add("LinkType",typeof(string));
dt.Columns.Add("Amount",typeof(decimal));
dt.Columns.Add("OpenAmount", typeof(decimal));
XDocument doc = XDocument.Load(FILENAME);
foreach (XElement billRet in doc.Descendants("BillRet"))
{
string txnID = (string)billRet.Element("TxnID");
DateTime timeCreated = (DateTime)billRet.Element("TimeCreated");
DateTime timeModified = (DateTime)billRet.Element("TimeModified");
long editSequence = (long)billRet.Element("EditSequence");
int TxnNumber = (int)billRet.Element("TxnNumber");
string vendorListID = (string)billRet.Element("VendorRef").Element("ListID");
string vendorFullName = (string)billRet.Element("VendorRef").Element("FullName");
string apAccountListID = (string)billRet.Element("APAccountRef").Element("ListID");
string apAccountFullName = (string)billRet.Element("APAccountRef").Element("FullName");
DateTime txnDate = (DateTime)billRet.Element("TxnDate");
DateTime dueDate = (DateTime)billRet.Element("DueDate");
string termsListID = (string)billRet.Element("TermsRef").Element("ListID");
string termsFullName = (string)billRet.Element("TermsRef").Element("FullName");
decimal amountDue = (decimal)billRet.Element("AmountDue");
decimal openAmount = (decimal)billRet.Element("OpenAmount");
Boolean isPaid = (Boolean)billRet.Element("IsPaid");
foreach (XElement linkedTxn in billRet.Elements("LinkedTxn"))
{
DataRow newRow = dt.Rows.Add();
newRow["TxnID"] = txnID;
newRow["TimeCreated"] = timeCreated;
newRow["TimeModified"] = timeModified;
newRow["EditSequence"] = editSequence;
newRow["TxnNumber"] = TxnNumber;
newRow["VendorListID"] = vendorListID;
newRow["VendorFullName"] = vendorFullName;
newRow["APAccountListID"] = apAccountListID;
newRow["APAccountFullName"] = apAccountFullName;
newRow["TxnDate"] = txnDate;
newRow["DueDate"] = dueDate;
newRow["TermsListID"] = termsListID ;
newRow["TermsFullName"] = termsFullName;
newRow["AmountDue"] = amountDue;
newRow["OpenAmount"] = openAmount;
newRow["IsPaid"] = isPaid;
newRow["LinkedTxnID"] = (string)linkedTxn.Element("TxnID");
newRow["LinkedTxnType"] = (string)linkedTxn.Element("TxnType");
newRow["LinkedTxnDate"] = (DateTime)linkedTxn.Element("TxnDate");
if(linkedTxn.Element("RefNumber") != null)
newRow["LinkedRefNumber"] = (int)linkedTxn.Element("RefNumber");
newRow["LinkType"] = (string)linkedTxn.Element("LinkType");
newRow["Amount"] = (decimal)linkedTxn.Element("Amount");
}
}
}
}
}