前一段时间做花旗的接口,返回的xml格式是带有命名空间的,可是难倒了我,找了好久才找到解决办法,给大家分享下,少走弯路。

1,直接进入正题,先看一段带有命名空间的xml,这段xml大概的意思是,前面是header信息,后面是body中的信息,body中有不少的节点,主要的是

<ns6:RewardDetail>节点,这个节点包含了很多的<ns8:RedemptionDetails>节点,和一个<ns8:CurrentRewardStatementDetails>节点以及后续以ns8开头的节点。
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ns0:RsHeader xmlns:ns0="http://www.citi.com/gcgi/shared/system/header">
<ns0:DateAndTimeStamp>2014-05-29T02:17:07.634-04:00</ns0:DateAndTimeStamp>
<ns0:UUID>55555461</ns0:UUID>
<ns0:ESBUUID>8dc47a89-3e3b-4b17-a631-938b848447ed</ns0:ESBUUID>
<ns0:ResponseCode>EC_6000_0</ns0:ResponseCode>
</ns0:RsHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns6:RewardDetailInqRs xmlns:ns="http://www.citi.com/gcgi/services/common/addressandcontact/v5_5_0_0" xmlns:ns5="http://www.citi.com/gcgi_prov/services/cards/reward/v6_0_0_0" xmlns:ns6="http://www.citi.com/gcgi/services/cards/reward/v6_0_0_0" xmlns:pfx="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns7="http://www.citi.com/gcgi_prov/entities/cards/reward/v6_0_0_0" xmlns:ns8="http://www.citi.com/gcgi/entities/cards/reward/v6_0_0_0" xmlns:ns2="http://www.citi.com/gcgi/entities/common/customer/v5_9_0_0" xmlns:pd="http://xmlns.tibco.com/bw/process/2003" xmlns:ns1="http://www.citi.com/gcgi/services/common/addressandcontact/v6_0_0_0" xmlns:ns4="http://www.citi.com/gcgi/shared/system/systemtypes" xmlns:ns3="http://www.citi.com/gcgi/shared/system/header" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pfx2="http://www.citi.com/gcgi/entities/common/customer/v6_0_0_0">
<ns6:Base>
<ns3:RsHeader>
<ns3:DateAndTimeStamp>2014-05-29T02:17:07.634-04:00</ns3:DateAndTimeStamp>
<ns3:UUID>55555461</ns3:UUID>
<ns3:ESBUUID>8dc47a89-3e3b-4b17-a631-938b848447ed</ns3:ESBUUID>
<ns3:ResponseCode>EC_6000_0</ns3:ResponseCode>
</ns3:RsHeader>
<ns6:RewardDetail>
<ns8:RedemptionDetails>
<ns8:RedemptionNo>99798493499998</ns8:RedemptionNo>
<ns8:RedemptionStatus>F</ns8:RedemptionStatus>
<ns8:RedemptionDetails>
<ns8:RedemptionNo>99798496299998</ns8:RedemptionNo>
<ns8:RedemptionStatus>F</ns8:RedemptionStatus>
<ns8:RedemptionMiniDetails>
<ns8:RedemptionProgram>1</ns8:RedemptionProgram>
<ns8:RedemptionBonusProgram>0</ns8:RedemptionBonusProgram>
<ns8:RedemptionLevel>00006</ns8:RedemptionLevel>
<ns8:RedemptionQuantity>99900</ns8:RedemptionQuantity>
</ns8:RedemptionMiniDetails>
<ns8:RedemptionPoints>9.9995E8</ns8:RedemptionPoints>
<ns8:RedemptionAmount>0.0E0</ns8:RedemptionAmount>
<ns8:RedemptionDescription>50 RMB CASH REBATE</ns8:RedemptionDescription>
<ns8:CreatorId>RWBGBD08</ns8:CreatorId>
<ns8:RedemptionDate>2015-02-06</ns8:RedemptionDate>
</ns8:RedemptionDetails>
<ns8:RedemptionDetails>
<ns8:RedemptionNo>99798499299998</ns8:RedemptionNo>
<ns8:RedemptionStatus>F</ns8:RedemptionStatus>
<ns8:RedemptionMiniDetails>
<ns8:RedemptionProgram>1</ns8:RedemptionProgram>
<ns8:RedemptionBonusProgram>0</ns8:RedemptionBonusProgram>
<ns8:RedemptionLevel>00006</ns8:RedemptionLevel>
<ns8:RedemptionQuantity>2480</ns8:RedemptionQuantity>
</ns8:RedemptionMiniDetails>
<ns8:RedemptionPoints>1.24E6</ns8:RedemptionPoints>
<ns8:RedemptionAmount>0.0E0</ns8:RedemptionAmount>
<ns8:RedemptionDescription>50 RMB CASH REBATE</ns8:RedemptionDescription>
<ns8:CreatorId>RWBGBD08</ns8:CreatorId>
<ns8:RedemptionDate>2015-01-07</ns8:RedemptionDate>
</ns8:RedemptionDetails>
<ns8:CurrentRewardStatementDetails>
<ns8:Sales>0.0E0</ns8:Sales>
<ns8:Beginning>9.80000008E10</ns8:Beginning>
<ns8:Earned>0.0E0</ns8:Earned>
<ns8:Bonus>0.0E0</ns8:Bonus>
<ns8:Adjusted>0.0E0</ns8:Adjusted>
<ns8:Redeemed>1.914E5</ns8:Redeemed>
<ns8:Available>9.79998094E10</ns8:Available>
</ns8:CurrentRewardStatementDetails>
<ns8:EnrolProgram>1</ns8:EnrolProgram>
<ns8:Description>YUPPIES</ns8:Description>
<ns8:EnrolmentIndicator>Y</ns8:EnrolmentIndicator>
<ns8:FlagTime>00:00:00</ns8:FlagTime>
<ns8:MembershipStatus>A</ns8:MembershipStatus>
</ns6:RewardDetail>
</ns6:Base>
</ns6:RewardDetailInqRs>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

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('_')[] == "" ? "" : code.InnerText.Split('_')[]; ; 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<RedemptionDetails> ListDetail = new List<RedemptionDetails>();
for (int i = ; 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,本例子到此结束,没有什么高深的东西,很简单的东西可能之前没用过而已,希望对大家有所帮助,这也是我现实中遇到的问题,和大家分享下。

[email protected]

05-19 21:31