首页 新闻 会员 周边

xml 反序列化问题,求帮看下

0
悬赏园豆:10 [待解决问题]

这是我所要解析的xml报文:

<?xml version="1.0" encoding="UTF-8"?><wmsInventoryBalancePushInfo>
  <status>推送状态</status>
<inventory_version>库存版本</inventory_version>
<inventory_time>库存时间</inventory_time>
  <list>
    <item>
       <id>记录号</id>
      <company>货主</company>
      <warehouse>仓库</warehouse>
      <item>商品编号</item>
      <on_hand_qty>在库库存数量</on_hand_qty>
      <lot>批号</lot>
      <expiration_date>有效期</expiration_date>
      <inventorySts>库存状态</inventorySts>
      <user_def1>预留字段</user_def1>
      <user_def2>预留字段</user_def2>
      <user_def3>预留字段</user_def3>
      <user_def4>预留字段</user_def4>
      <user_def5>预留字段</user_def5>
      <user_def6>预留字段</user_def6>
      <user_def7>预留字段</user_def7>
      <user_def8>预留字段</user_def8>
    </item>
  </list>
</wmsInventoryBalancePushInfo>

实体构造如下:

wmsInventoryBalancePushInfo
 public class wmsInventoryBalancePushInfo
    {
        public string status { get; set; }

        public string inventory_version { get; set; }

        public string inventory_time { get; set; }

        [XmlElement("item")]
        public List<wmsInventoryBalancePushItemInfo> list { get; set; }
    }

wmsInventoryBalancePushItemInfo:

public class wmsInventoryBalancePushItemInfo
    {
        public string company { get; set; }

        public string warehouse { get; set; }

        public string item { get; set; }

        public decimal on_hand_qty { get; set; }

        public string lot { get; set; }

        public string expiration_date { get; set; }

        public string inventorySts { get; set; }
    }

解析的方法:

public static object Deserialize(string Xml, Type ThisType)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(ThisType);
            object result;
            try
            {
                using (StringReader stringReader = new StringReader(Xml))
                {
                    result = xmlSerializer.Deserialize(stringReader);
                }
            }
            catch (Exception innerException)
            {
                bool flag = false;
                if (Xml != null)
                {
                    if (Xml.StartsWith(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble())))
                    {
                        flag = true;
                    }
                }
                throw new ApplicationException(string.Format("Couldn't parse XML: '{0}'; Contains BOM: {1}; Type: {2}.", Xml, flag, ThisType.FullName), innerException);
            }
            return result;
        }  
View Code

方法调用:

object b= Deserialize(xml,typeof(wmsInventoryBalancePushInfo));

 

得到的结果是拿到了下面这些节点的值

<status>推送状态</status>
<inventory_version>库存版本</inventory_version>
<inventory_time>库存时间</inventory_time>

List节点包含无法获取,对 xml反序列化的经验比较少,求指教

许大虾的主页 许大虾 | 初学一级 | 园豆:12
提问于:2015-10-14 21:12
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册