十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1,直接进入正题,先看一段带有命名空间的xml,这段xml大概的意思是,前面是header信息,后面是body中的信息,body中有不少的节点,主要的是
创新互联公司是一家集成都网站制作、成都网站设计、外贸营销网站建设、网站页面设计、网站优化SEO优化为一体的专业的建站公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。节点,这个节点包含了很多的节点,和一个节点以及后续以ns8开头的节点。
2014-05-29T02:17:07.634-04:00 55555461 8dc47a89-3e3b-4b17-a631-938b848447ed EC_6000_0 2014-05-29T02:17:07.634-04:00 55555461 8dc47a89-3e3b-4b17-a631-938b848447ed EC_6000_0 99798493499998 F 99798496299998 F 1 0 00006 99900 9.9995E8 0.0E0 50 RMB CASH REBATE RWBGBD08 2015-02-06 99798499299998 F 1 0 00006 2480 1.24E6 0.0E0 50 RMB CASH REBATE RWBGBD08 2015-01-07 0.0E0 9.80000008E10 0.0E0 0.0E0 0.0E0 1.914E5 9.79998094E10 1 YUPPIES Y 00:00:00 A
2,这一步我们,要开始解析xml,当然准备工作也是要做的,命名空间一定要引用using System.Xml,怎么处理命名空间这一步也将处理。
实例化一个xml命名空间管理器,然后将不同的命名空间加入管理器中,这些命名空间我们从哪里找呢?很显然我们要从xml文档中把他们找出来
xml文档中只要是带有这种 (xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/")的都是命名空间,要把解析时候要用的
命名空间加入命名空间管理器。
XmlDocument doc = new XmlDocument();
doc.LoadXml(“xml字符串”);
XmlNamespaceManager nsMgr= new XmlNamespaceManager(doc.NameTable);//这一步实例化一个xml命名空间管理器nsMgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
nsMgr.AddNamespace("ns", "http://www.citi.com/gcgi/services/common/addressandcontact/v5_5_0_0");
nsMgr.AddNamespace("ns0", "http://www.citi.com/gcgi/shared/system/header");
nsMgr.AddNamespace("ns1", "http://www.citi.com/gcgi/services/common/addressandcontact/v6_0_0_0");
nsMgr.AddNamespace("ns2", "http://www.citi.com/gcgi/entities/common/customer/v5_9_0_0");
nsMgr.AddNamespace("ns3", "http://www.citi.com/gcgi/shared/system/header");
nsMgr.AddNamespace("ns4", "http://www.citi.com/gcgi/shared/system/systemtypes");
nsMgr.AddNamespace("ns5", "http://www.citi.com/gcgi_prov/services/cards/reward/v6_0_0_0");
nsMgr.AddNamespace("ns6", "http://www.citi.com/gcgi/services/cards/reward/v6_0_0_0");
nsMgr.AddNamespace("ns7", "http://www.citi.com/gcgi_prov/entities/cards/reward/v6_0_0_0");
nsMgr.AddNamespace("ns8", "http://www.citi.com/gcgi/entities/cards/reward/v6_0_0_0");
3,为了接收xml中的值,我新建了一个类
public class RedemptionDetails
{
public string RedemptionNo { get; set; }
public string RedemptionStatus { get; set; }
public string RedemptionPoints { get; set; }
public string RedemptionAmount { get; set; }
public string RedemptionDescription { get; set; }
public string CreatorId { get; set; }
public string RedemptionDate { get; set; }
public string RedemptionProgram { get; set; }
public string RedemptionBonusProgram { get; set; }
public string RedemptionLevel { get; set; }
public string RedemptionQuantity { get; set; }
}
4,下面的代码是我具体的实现解析到我需要的具体值,这些也不做过多的解释,直接上代码
XmlNode ss = doc.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/ns6:RewardDetailInqRs/ns6:Base", nsMgr);
if (ss != null)
{
XmlNode code= ss.SelectSingleNode("ns3:RsHeader/ns3:ResponseCode", nsMgr);
detail.ResponseCode= code.InnerText.Split('_')[2] == "0" ? "0000" : code.InnerText.Split('_')[2]; ;
XmlNode current= ss.SelectSingleNode("ns6:RewardDetail", nsMgr).SelectSingleNode("ns8:CurrentRewardStatementDetails", nsMgr);
string Avaliable = current.SelectSingleNode("ns8:Available", nsMgr).InnerText;
detail.Available= ConvertToPoint(Avaliable);
string Redeemed = current.SelectSingleNode("ns8:Redeemed", nsMgr).InnerText;
detail.Redeemed= ConvertToPoint(Redeemed);
XmlNodeList RedemptionDetailsList= ss.SelectSingleNode("ns6:RewardDetail", nsMgr).SelectNodes("ns8:RedemptionDetails", nsMgr);
List ListDetail = new List();
for (int i = 0; i < RedemptionDetailsList.Count; i++)
{
RedemptionDetails redemptionDetails= new RedemptionDetails();
redemptionDetails.RedemptionNo= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionNo", nsMgr).InnerText;
if (RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionStatus", nsMgr) != null)
{
redemptionDetails.RedemptionStatus= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionStatus", nsMgr).InnerText;
}
else
{
redemptionDetails.RedemptionStatus= "";
}
string RedemptionPoints = RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionPoints", nsMgr).InnerText;
redemptionDetails.RedemptionPoints= ConvertToPoint(RedemptionPoints);
string RedemptionAmount = RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionAmount", nsMgr).InnerText;
redemptionDetails.RedemptionAmount=ConvertToPoint(RedemptionAmount);
redemptionDetails.RedemptionDescription= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionDescription", nsMgr).InnerText;
redemptionDetails.CreatorId= RedemptionDetailsList[i].SelectSingleNode("ns8:CreatorId", nsMgr).InnerText;
redemptionDetails.RedemptionDate= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionDate", nsMgr).InnerText;
redemptionDetails.RedemptionProgram= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionMiniDetails/ns8:RedemptionProgram", nsMgr).InnerText;
redemptionDetails.RedemptionBonusProgram= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionMiniDetails/ns8:RedemptionBonusProgram", nsMgr).InnerText;
redemptionDetails.RedemptionLevel= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionMiniDetails/ns8:RedemptionLevel", nsMgr).InnerText;
redemptionDetails.RedemptionQuantity= RedemptionDetailsList[i].SelectSingleNode("ns8:RedemptionMiniDetails/ns8:RedemptionQuantity", nsMgr).InnerText;
ListDetail.Add(redemptionDetails);
}
detail.ListDetail= ListDetail;
}
5,本例子到此结束,没有什么高深的东西,很简单的东西可能之前没用过而已,希望对大家有所帮助,这也是我现实中遇到的问题,和大家分享下。
shixudong3@yeah.net