/// <summary> /// 把ocr返回的xml数据 解析成对象 /// </summary> /// <param name="xml"></param> private void OcrXmlToHddz(string xml, out yw_hddzEntity model, out List <yw_hddz_spxxEntity> list) { model = new yw_hddzEntity(); list = new List <yw_hddz_spxxEntity>(); string strError = ""; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlNodeList xnl = doc.GetElementsByTagName("yw_hddz"); if (xnl.Count == 0) { throw new Exception("返回的xml中不包含yw_hddz"); } XmlNode mainxn = xnl.Item(0); //string jsonText = JsonConvert.SerializeXmlNode(mainxn); //JObject obj = JsonConvert.DeserializeObject(jsonText) as JObject; //if (obj["yw_hddz"]["invoice_date"] != null)//发票日期格式判定 //{ // string str = obj["yw_hddz"]["invoice_date"].ToString().Replace("\"", ""); // DateTime d = new DateTime(); // if (!DateTime.TryParse(str, out d)) // { // throw new Exception("OCR返回的发票日期为" + str + "格式错误,日期格式为yyyy.MM.dd!"); // } //} // yw_hddzXmlData data = JsonConvert.DeserializeObject<yw_hddzXmlData>(jsonText); model = ModelTools.ConvertXmlToModel <yw_hddzEntity>(mainxn.OuterXml, out strError); if (!string.IsNullOrEmpty(strError)) { throw new Exception("返回的xml主表yw_hddz解析错误!详细:" + strError); } XmlNodeList detailsxnl = mainxn.SelectNodes("details/yw_hddz_spxx"); var i = 1; foreach (XmlNode spxn in detailsxnl) { //string jsonText1 = JsonConvert.SerializeXmlNode(spxn); yw_hddz_spxxEntity spitem = ModelTools.ConvertXmlToModel <yw_hddz_spxxEntity>(spxn.OuterXml, out strError); //yw_hddz_spxxXmlData data1 = JsonConvert.DeserializeObject<yw_hddz_spxxXmlData>(jsonText1); if (string.IsNullOrEmpty(strError)) { //spitem = data1.yw_hddz_spxx; //spitem.ywbh = ywbh; spitem.cxh = i++; spitem.sppp = spitem.sppp_yw; spitem.sppz = spitem.sppz_yw; spitem.spdj = spitem.spdj_yw; spitem.spgg = spitem.spgg_yw; list.Add(spitem); } else { throw new Exception("返回的xml从表yw_hddz_spxx解析错误!详细:" + strError); } } }