我有这段XML,我一直在努力编写一个C#或一个VB.NET,它允许我循环抛出每个ROWDETAIL(父节点)访问其节点的innertext值。 BATCH(子节点)访问innertext值。
这是我到目前为止所拥有的
Dim nodes_rows As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/PURCHASE_INVOICE/IDOC/ROWDETAIL")
Dim nodes_batchs As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/PURCHASE_INVOICE/IDOC/ROWDETAIL/BATCH")
For Each nodes_row As XmlNode In nodes_rows
ITEMCODE = nodes_row.SelectSingleNode("ITEMCODE").InnerText
DSCRIPTION = nodes_row.SelectSingleNode("DSCRIPTION").InnerText
For Each nodes_batch As XmlNode In nodes_batchs
BATCHNUM = nodes_batch.SelectSingleNode("BATCHNUM").InnerText
BATCHQTTY = nodes_batch.SelectSingleNode("BATCHQTTY").InnerText
Next
Next
<ROWDETAIL>
<ITEMCODE>1000000</ITEMCODE>
<DSCRIPTION>Tablet PC 64GB White</DSCRIPTION>
<QUANTITY>18</QUANTITY>
<WHSCODE>01</WHSCODE>
<UNITPRICE>700</UNITPRICE>
<VATGROUP>I1</VATGROUP>
<PRICEAFTERDISCOUNT>600</PRICEAFTERDISCOUNT>
<BATCHFLAG>YES</BATCHFLAG>
<BATCH>
<BATCHNUM>A1SD</BATCHNUM>
<BATCHQTTY>18</BATCHQTTY>
<EXPDATE>02.11.17</EXPDATE>
</BATCH>
</ROWDETAIL>
<ROWDETAIL>
<ITEMCODE>20000000</ITEMCODE>
<DSCRIPTION>Tablet PC 64GB White</DSCRIPTION>
<QUANTITY>18</QUANTITY>
<WHSCODE>01</WHSCODE>
<UNITPRICE>700</UNITPRICE>
<VATGROUP>I1</VATGROUP>
<PRICEAFTERDISCOUNT>600</PRICEAFTERDISCOUNT>
<BATCHFLAG>YES</BATCHFLAG>
<BATCH>
<BATCHNUM>A1SD</BATCHNUM>
<BATCHQTTY>12</BATCHQTTY>
<EXPDATE>02.11.17</EXPDATE>
</BATCH>
<BATCH>
<BATCHNUM>A1SD</BATCHNUM>
<BATCHQTTY>6</BATCHQTTY>
<EXPDATE>02.11.17</EXPDATE>
</BATCH>
</ROWDETAIL>
答案 0 :(得分:0)
使用xsd2Code让您的生活更轻松,在为XML生成架构后,该工具将从中生成类甚至是序列化方法。
答案 1 :(得分:0)
您应该删除nodes_batchs并使用nodes_row选择子项。
Dim nodes_rows As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/PURCHASE_INVOICE/IDOC/ROWDETAIL")
For Each nodes_row As XmlNode In nodes_rows
ITEMCODE = nodes_row.SelectSingleNode("ITEMCODE").InnerText
DSCRIPTION = nodes_row.SelectSingleNode("DSCRIPTION").InnerText
For Each nodes_batch As XmlNode In nodes_row.SelectNodes("BATCH")
BATCHNUM = nodes_batch.SelectSingleNode("BATCHNUM").InnerText
BATCHQTTY = nodes_batch.SelectSingleNode("BATCHQTTY").InnerText
Next
Next
如果批次只出现一次详细信息,则根本不必使用。您可以替换每个:
BATCHNUM = nodes_row.SelectSingleNode("BATCH/BATCHNUM").InnerText
BATCHQTTY = nodes_row.SelectSingleNode("BATCH/BATCHQTTY").InnerText
答案 2 :(得分:0)
尝试XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("ROWDETAIL").Select(x => new {
itemCode = x.Element("ITEMCODE").Value,
description = x.Element("DSCRIPTION").Value,
quantity = (int)x.Element("QUANTITY"),
code = x.Element("WHSCODE").Value,
price = x.Element("UNITPRICE").Value,
group = x.Element("VATGROUP").Value,
discount = x.Element("PRICEAFTERDISCOUNT").Value,
batchFlag = x.Element("BATCHFLAG").Value,
batches = x.Elements("BATCH").Select(y => new {
number = y.Element("BATCHNUM").Value,
quantity = (int)y.Element("BATCHQTTY"),
expDate = DateTime.ParseExact(y.Element("EXPDATE").Value, "MM.dd.yy", CultureInfo.InvariantCulture)
}).ToList()
}).ToList();
}
}
}
答案 3 :(得分:0)
尝试使用XElement(System.XML.Linq)
'to load XMLdta from a source see
'one of the XElement.Load methods
Dim XMLdta As XElement = <rows>
<ROWDETAIL>
<ITEMCODE>1000000</ITEMCODE>
<DSCRIPTION>Tablet PC 64GB White</DSCRIPTION>
<QUANTITY>18</QUANTITY>
<WHSCODE>01</WHSCODE>
<UNITPRICE>700</UNITPRICE>
<VATGROUP>I1</VATGROUP>
<PRICEAFTERDISCOUNT>600</PRICEAFTERDISCOUNT>
<BATCHFLAG>YES</BATCHFLAG>
<BATCH>
<BATCHNUM>A1SD</BATCHNUM>
<BATCHQTTY>18</BATCHQTTY>
<EXPDATE>02.11.17</EXPDATE>
</BATCH>
</ROWDETAIL>
<ROWDETAIL>
<ITEMCODE>20000000</ITEMCODE>
<DSCRIPTION>Tablet PC 64GB White</DSCRIPTION>
<QUANTITY>18</QUANTITY>
<WHSCODE>01</WHSCODE>
<UNITPRICE>700</UNITPRICE>
<VATGROUP>I1</VATGROUP>
<PRICEAFTERDISCOUNT>600</PRICEAFTERDISCOUNT>
<BATCHFLAG>YES</BATCHFLAG>
<BATCH>
<BATCHNUM>A1SD</BATCHNUM>
<BATCHQTTY>12</BATCHQTTY>
<EXPDATE>02.11.17</EXPDATE>
</BATCH>
<BATCH>
<BATCHNUM>A1SD</BATCHNUM>
<BATCHQTTY>6</BATCHQTTY>
<EXPDATE>02.11.17</EXPDATE>
</BATCH>
</ROWDETAIL>
</rows>
For Each xe As XElement In XMLdta.Elements
Dim ITEMCODE As String = xe...<ITEMCODE>.Value
Dim DSCRIPTION As String = xe...<DSCRIPTION>.Value
For Each bxe As XElement In xe...<BATCH>
Dim BATCHNUM As String = bxe...<BATCHNUM>.Value
Dim BATCHQTTY As String = bxe...<BATCHQTTY>.Value
Dim expDt As String = bxe...<EXPDATE>.Value
Next
Next