1,直接進(jìn)入正題,先看一段帶有命名空間的xml,這段xml大概的意思是,前面是header信息,后面是body中的信息,body中有不少的節(jié)點(diǎn),主要的是
創(chuàng)新互聯(lián)公司是一家集成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站頁面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)的建站公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠(chéng)信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場(chǎng)環(huán)境中,互促共生。節(jié)點(diǎn),這個(gè)節(jié)點(diǎn)包含了很多的節(jié)點(diǎn),和一個(gè)節(jié)點(diǎn)以及后續(xù)以ns8開頭的節(jié)點(diǎn)。
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,當(dāng)然準(zhǔn)備工作也是要做的,命名空間一定要引用using System.Xml,怎么處理命名空間這一步也將處理。
實(shí)例化一個(gè)xml命名空間管理器,然后將不同的命名空間加入管理器中,這些命名空間我們從哪里找呢?很顯然我們要從xml文檔中把他們找出來
xml文檔中只要是帶有這種 (xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/")的都是命名空間,要把解析時(shí)候要用的
命名空間加入命名空間管理器。
XmlDocument doc = new XmlDocument();
doc.LoadXml(“xml字符串”);
XmlNamespaceManager nsMgr= new XmlNamespaceManager(doc.NameTable);//這一步實(shí)例化一個(gè)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中的值,我新建了一個(gè)類
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,下面的代碼是我具體的實(shí)現(xiàn)解析到我需要的具體值,這些也不做過多的解釋,直接上代碼
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,本例子到此結(jié)束,沒有什么高深的東西,很簡(jiǎn)單的東西可能之前沒用過而已,希望對(duì)大家有所幫助,這也是我現(xiàn)實(shí)中遇到的問題,和大家分享下。
shixudong3@yeah.net