解析多个结点的XML文件,格式如下:
<?xml version="1.0" encoding="utf-8"?> <response> <flag>success</flag> <code>1</code> <message>成功单数:1 失败单数:0</message> <items> <item> <wareCode>ABC</wareCode> <itemCode>1000100</itemCode> <itemId>11111236</itemId> <inventoryType>ZP</inventoryType> <quantity>235</quantity> <lockQuantity>0</lockQuantity> </item> </items> </response>
解析报文代码:
先取出response中的结点进行遍历,然后再取出item中的结点遍历。
var strResponse = from itemResponse in xDoc.Descendants("response") select itemResponse; foreach (var items in strResponse) { string status = items.Element("flag").Value; if (status == "success") { var itemResponse = from itemQuantity in xDoc.Descendants("item") select itemQuantity; foreach (var item in itemResponse) { obj.PRODUCT_UPC = item.Element("itemCode").Value; obj.PRODUCT_KD_PRODCODE = item.Element("itemId").Value; obj.QUANTITY = Convert.ToInt64(item.Element("quantity").Value); obj.PRODUCT_ID = prod_kd.PRODUCT_ID; obj.LOCKQUANTITY = Convert.ToInt64(item.Element("lockQuantity").Value); obj.PRODUCT_CODE = prod_kd.PRODUCT_CODE; obj.PRODUCT_NAME = prod_kd.PRODUCT_NAME; } } }