[英]Get list of line items from XML
I have an eConnect outgoing document pulled from MSMQ, i need to loop through the line items. 我有一个从MSMQ提取的eConnect外发文档,我需要遍历这些行项目。 I've tried:
我试过了:
XmlNodeList nodes = root.SelectNodes("/Sales_History_Transaction/eConnect/SO_Hist_Trans/Line");
(and several other attempts) with no success... (以及其他几次尝试)都没有成功...
Here is the XML, how can i get a collection of Line items from the "Line" nodes so that i can then get the line item details? 这是XML,如何从“行”节点中获取行项目的集合,以便随后可以获取行项目详细信息?
<Sales_History_Transaction:root>
<eConnect ACTION="1" Requester_DOCTYPE="Sales_History_Transaction" DBNAME="TWO" TABLENAME="SOP30200" DATE1="2013-05-03T09:24:09.970" SOPNUMBE="999999" SOPTYPE="3">
<SO_Hist_Trans>
<SOPNUMBE>999999</SOPNUMBE>
<SOPTYPE>3</SOPTYPE>
<Line>
<CMPNTSEQ>0</CMPNTSEQ>
<LNITMSEQ>998777</LNITMSEQ>
<ITEMNMBR>0099999</ITEMNMBR>
<ITEMDESC>Laptop</ITEMDESC>
</Line>
<Line>
<CMPNTSEQ>0</CMPNTSEQ>
<LNITMSEQ>777</LNITMSEQ>
<ITEMNMBR>0099</ITEMNMBR>
<ITEMDESC>Desktop</ITEMDESC>
</Line>
<Line>
<CMPNTSEQ>0</CMPNTSEQ>
<LNITMSEQ>679777</LNITMSEQ>
<ITEMNMBR>0569</ITEMNMBR>
<ITEMDESC>Memory</ITEMDESC>
</Line>
</SO_Hist_Trans>
</eConnect>
</Sales_History_Transaction:root>
Your xml is not well-formed. 您的xml格式不正确。
The root tag seems to consist of an undeclared namespace Sales_History_Transaction
and the element name root
. 根标记似乎由未声明的命名空间
Sales_History_Transaction
和元素名称root
。 Have you missed the line in which Sales_History_Transaction
is defined? 您是否错过了定义
Sales_History_Transaction
的行?
Once you have valid xml, it should be as simple (depending on namespaces) as: 拥有有效的xml之后,它应该和以下内容一样简单(取决于名称空间):
var xdoc = XDocument.Parse(yourXml);
var nodes = xdoc.Descendants("Line");
Does this do the trick for your example? 这对您的示例有用吗? Or is this the same you tried that failed?
还是您尝试失败的方法相同?
XmlDocument doc1 = new XmlDocument();
doc1.Load("yoururl"); //I don't know from where you load
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Sales_History_Transaction/eConnect/SO_Hist_Trans/Line");
foreach (XmlNode node in nodes) {
Console.Out.WriteLine(node["CMPNTSEQ"].InnerText);
Console.Out.WriteLine(node["LNITMSEQ"].InnerText);
Console.Out.WriteLine(node["ITEMNMBR"].InnerText);
Console.Out.WriteLine(node["ITEMDESC"].InnerText);
Console.Out.WriteLine("------------------------");
}
Figured it out: 弄清楚了:
XmlNodeList nodes = xmlDocument.GetElementsByTagName("Line");
foreach (XmlNode node in nodes)
{
string txt = node["ElementName"].InnerText;
}
this enumerates through all the "Line" elements in the XML. 它枚举了XML中的所有“行”元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.