在Asp.Net中以XML格式创建请求

时间:2013-08-25 17:33:47

标签: asp.net xmlhttprequest

我需要使用XMLDocument创建一个请求,该请求具有一些特定的值,而我无法实现。我的xml应该是这样的;

<?xml version="1.0" encoding="ISO-8859-9" ?> 
<ePaymentMsg VersionInfo="2.0" TT="Request" RM="Direct" CT="Money">
  <Operation ActionType="Sale">
    <OpData>
      <MerchantInfo MerchantId="006100" MerchantPassword="123" />
      <ActionInfo>
        <TrnxCommon TrnxID="">
          <AmountInfo Amount="1.00" Currency="949" />
        </TrnxCommon>
        <PaymentTypeInfo>
          <InstallmentInfo NumberOfInstallments="0"/>
        </PaymentTypeInfo>
      </ActionInfo>
      <PANInfo PAN="402275******5574" ExpiryDate="201406" CVV2="***" BrandID="MASTER" />
      <OrgTrnxInfo /> 
      <CardHolderIP>127.0.0.1</CardHolderIP>
    </OpData>
  </Operation> 
</ePaymentMsg>

任何人都可以帮我使用C#XmlDocument创建这个xml,XmlNode

我试过了,

 XmlNode node = null;
            XmlDocument _msgTemplate = new XmlDocument();
            _msgTemplate.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><ePaymentMsg VersionInfo=\"2.0\" TT=\"Request\" RM=\"Direct\" CT=\"Money\">" +
                "<Operation ActionType=\"Sale\"><OpData><MerchantInfo MerchantId=\"\" MerchantPassword=\"\" />" +
                "<ActionInfo><TrnxCommon TrnxID=\"\" Protocol=\"156\"><AmountInfo Amount=\"0\" Currency=\"792\" /></TrnxCommon><PaymentTypeInfo>" +
                "<InstallmentInfo NumberOfInstallments=\"0\" /></PaymentTypeInfo></ActionInfo><PANInfo PAN=\"\" ExpiryDate=\"\" CVV2=\"\" BrandID=\"\" />" +
                "<OrderInfo><OrderLine>0</OrderLine></OrderInfo><OrgTrnxInfo /><CustomData></CustomData><CardHolderIp></CardHolderIp></OpData></Operation></ePaymentMsg>");
            node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/MerchantInfo");
            node.Attributes["MerchantId"].Value = "006100";
            node.Attributes["MerchantPassword"].Value = "123";
            node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/ActionInfo/TrnxCommon");
            node.Attributes["TrnxID"].Value = Guid.NewGuid().ToString();
            node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/ActionInfo/TrnxCommon/AmountInfo");
            string gonderilecekAmount = amount.ToString("####.00");
            gonderilecekAmount = gonderilecekAmount.Replace(",", ".");
            node.Attributes["Amount"].Value = gonderilecekAmount;
            node.Attributes["Currency"].Value = "949";
            node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/ActionInfo/PaymentTypeInfo/InstallmentInfo");
            node.Attributes["NumberOfInstallments"].Value = "0";
            node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/PANInfo");
            node.Attributes["PAN"].Value = "402275******5574";
            node.Attributes["ExpiryDate"].Value = "201406";
            node.Attributes["CVV2"].Value = "***";
            node.Attributes["BrandID"].Value = "VISA";
            node = _msgTemplate.SelectSingleNode("//ePaymentMsg/Operation/OpData/CardHolderIp");
            node.Attributes["CardHolderIp"].Value = "10.20.30.40";
            request = _msgTemplate.OuterXml;
            return request;

我认为CardHolderIp节点出了问题。任何帮助都会有用。

1 个答案:

答案 0 :(得分:0)

 node.Attributes["CardHolderIp"].Value = "10.20.30.40";

行,我把它改为

 node.InnerText= "10.20.30.40";