码迷,mamicode.com
首页 > Windows程序 > 详细

C# 解析XML格式的字符串

时间:2017-07-15 10:06:43      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:nap   get   ldo   row   stream   except   字符串   cep   XML   

  public CreateOrderReturnResult GetCreateOrderReturnApi()
        {
            var result = new CreateOrderReturnResult();
            try
            {
                DataSet ds = new DataSet();
                StringReader stream = new StringReader("XML字符串");//读取字符串为数据量
                XmlTextReader reader = new XmlTextReader(stream);//对XML的数据流的只进只读访问
                ds.ReadXml(reader);//吧数据读入DataSet
                DataTable dt = ds.Tables["createOrderReturn"];

                result.Orders = new List<CreateOrderReturn>();
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        CreateOrderReturn order = new CreateOrderReturn();
                        DataRow dr = ds.Tables[0].Rows[i];
                        order.AdditionalDocument = dr["additionalDocument"].ToString();
                        order.DeliveryCharge = decimal.Parse(dr["deliveryCharge"].ToString());
                        order.ErrMessage = dr["errMessage"].ToString();
                        order.InsurPermFee = decimal.Parse(dr["insurPermFee"].ToString());
                        order.ItemNo = dr["itemNo"].ToString();
                        order.OrderNo = dr["OrderNo"].ToString();
                        order.Status = int.Parse(dr["Status"].ToString());
                        result.Orders.Add(order);
                    }
                }
            }
            catch (Exception ex)
            {
                result.HasError = true;
                result.ErrorMessage = ex.Message;
            }
            return result;
        }


   public class CreateOrderReturnResult
    {
        public bool HasError { get; set; }
        public string ErrorMessage { get; set; }
        public List<CreateOrderReturn> Orders { get; set; }
    }

    public class CreateOrderReturn
    {
        public string AdditionalDocument { get; set; }
        public decimal DeliveryCharge { get; set; }
        public string ErrMessage { get; set; }
        public decimal InsurPermFee { get; set; }
        public string ItemNo { get; set; }
        public string OrderNo { get; set; }
        public int Status { get; set; }
    }

C# 解析XML格式的字符串

标签:nap   get   ldo   row   stream   except   字符串   cep   XML   

原文地址:http://www.cnblogs.com/kevin860/p/7181105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!