XML操作示例

下面用到的是常见的操作,当然也可以封闭到一个类中。(实际上网上也确实有这个的代码)

 
   
   
     <?xml version="1.0" encoding="utf-8"?>
<ProvBOSS>
<BIPCode>BIP9B985</BIPCode>
<BIPVer>0100</BIPVer>
<ActivityCode>T9000985</ActivityCode>
<ActionCode>0</ActionCode>
<ProcID></ProcID>
<TransIDO></TransIDO>
<ProcessTime></ProcessTime>
<TestFlag>1</TestFlag>
<MsgSender></MsgSender>
<MsgReceiver></MsgReceiver>
<SvcContVer>0100</SvcContVer>
<SvcCont>
<![CDATA[
<BizProcReq>
<OprNumb>21542</OprNumb>
<MSISDN>13800000000</MSISDN>
<IMSI>235625485</IMSI>
<OprCode>06</OprCode>
<EfftTime>20081122000000</EfftTime>
<Extra></Extra>
</BizProcReq>
]]>
</SvcCont>
</ProvBOSS>


 
   
   
                 //加载为XML文档
XmlDocument Xml = new XmlDocument();
Xml.LoadXml(ReqData);
XmlNode root
= Xml.SelectSingleNode("/ProvBOSS");
#region 请求处理过程
//更改交易动作代码 0:请求,1:应答
XmlNode actioncode = Xml.SelectSingleNode("/ProvBOSS/ActionCode");
actioncode.InnerText
= "1";
//添加落地方交易流水号
XmlElement TransIDH = Xml.CreateElement("TransIDH");
TransIDH.InnerText
= DateTime.Now.ToString("yyyyMMddHHmmss");
XmlNode TransIDO
= Xml.SelectSingleNode("/ProvBOSS/TransIDO");
root.InsertAfter(TransIDH, TransIDO);
//更改应答方处理请求的时间
XmlNode ProcessTime = Xml.SelectSingleNode("/ProvBOSS/ProcessTime");
ProcessTime.InnerText
= DateTime.Now.ToString("yyyyMMddHHmmss");
//创建 应答/错误信息
XmlElement RspType = Xml.CreateElement("RspType");
XmlElement RspCode
= Xml.CreateElement("RspCode");
XmlElement RspDesc
= Xml.CreateElement("RspDesc");
XmlElement ResponseNode
= Xml.CreateElement("Response");
ResponseNode.AppendChild(RspType);
ResponseNode.AppendChild(RspCode);
ResponseNode.AppendChild(RspDesc);
RspType.InnerText
= "0";
RspCode.InnerText
= "0000";
RspDesc.InnerText
= "Success";
root.InsertAfter(ResponseNode, ProcessTime);
////////////////////////////////////
//请求/应答内容
////////////////////////////////////
XmlNode SvcCont = Xml.SelectSingleNode("/ProvBOSS/SvcCont");
if (SvcCont.FirstChild.NodeType == XmlNodeType.CDATA)
{
//获取ServiceContent中的值
XmlDocument xmlSvcCont = new XmlDocument();
xmlSvcCont.LoadXml(SvcCont.FirstChild.Value);
string OprNumb = xmlSvcCont.SelectSingleNode("BizProcReq/OprNumb").InnerText;
string MSISDN = xmlSvcCont.SelectSingleNode("BizProcReq/MSISDN").InnerText;
string IMSI = xmlSvcCont.SelectSingleNode("BizProcReq/IMSI").InnerText;
string OprCode = xmlSvcCont.SelectSingleNode("BizProcReq/OprCode").InnerText;
string EfftTime = xmlSvcCont.SelectSingleNode("BizProcReq/EfftTime").InnerText;
//创建响应报文体
XmlElement OprNumbNode = Xml.CreateElement("OprNumb");
XmlElement MSISDNNode
= Xml.CreateElement("MSISDN");
XmlElement EfftFlagNode
= Xml.CreateElement("EfftFlag");
XmlElement RsltNode
= Xml.CreateElement("Rslt");
XmlElement BizProcRsp
= Xml.CreateElement("BizProcRsp");

BizProcRsp.AppendChild(OprNumbNode);
BizProcRsp.AppendChild(MSISDNNode);
BizProcRsp.AppendChild(EfftFlagNode);
BizProcRsp.AppendChild(RsltNode);

OprNumbNode.InnerText
= OprNumb;
MSISDNNode.InnerText
= MSISDN;
EfftFlagNode.InnerText
= "0";
RsltNode.InnerText
= "";

//DB操作
try
{
//

}
catch (Exception ex)
{


}
//修改ServiceContent的值,以返回响应报文
SvcCont.FirstChild.Value = BizProcRsp.OuterXml;
}
else
{
//报文头里的
RspType.InnerText = "1";
RspCode.InnerText
= "0004";
RspDesc.InnerText
= "Service Content报文格式不正确";
}
#endregion
//返回数据给BOSS
ResData = Xml.OuterXml;


你可能感兴趣的:(xml)