前一段時間做花旗的接口,返回的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('_')[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<RedemptionDetails> ListDetail = new List<RedemptionDetails>(); 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,本例子到此結束,沒有什麼高深的東西,很簡單的東西可能之前沒用過而已,希望對大家有所幫助,這也是我現實中遇到的問題,和大家分享下。
string x12=doc.SelectSingleNode(@"Manifest/Head/MessageID")這樣對麼。。。
它能找到字符為 Manifest/Head/MessageID的節點麼
你想解析這個文件 做到什麼樣的效果?