c# - .NET C# 中的动态 XML 模板
问题描述
我正在尝试向在 C# 中使用 SOAP 的 Web 服务发出请求。我能够做到这一点,但我正在寻找一种更勤奋的方法来做到这一点。
我想从文件(模板)加载 XML 并将值动态分配给variables
.
我的C#:
FollowUpResponse followUpResult = new FollowUpResponse
{
Completed = false,
PaymentAccepted = false,
Message = string.Empty
};
Guid payId = Guid.Parse(payRequestId);
XmlDocument soapDoc = new XmlDocument();
Account account = new Account
{
PayGateId = BigInteger.Parse(_tools.GetPaygateId()),
Password = _tools.GetPaygatePassword()
};
soapDoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> " +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.paygate.co.za/PayHOST\">" +
"<SOAP-ENV:Body>" +
"<ns1:SingleFollowUpRequest>" +
"<ns1:QueryRequest>" +
"<ns1:Account>" +
$"<ns1:PayGateId>{account.PayGateId}</ns1:PayGateId>" +
$"<ns1:Password>{account.Password}</ns1:Password> " +
"</ns1:Account>" +
$"<ns1:PayRequestId>{payId}</ns1:PayRequestId>" +
"</ns1:QueryRequest>" +
"</ns1:SingleFollowUpRequest>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>");
HttpWebRequest httpWebRequest = _tools.CreateWebRequest();
Stream stream = httpWebRequest.GetRequestStream();
soapDoc.Save(stream);
而不是上面的,我想要下面的模板,并为这些变量赋值。
模板:
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope"
xmlns:ns1="http://www.paygate.co.za/PayHOST">
<SOAP-ENV:Body>
<ns1:SingleFollowUpRequest>
<ns1:QueryRequest>
<ns1:Account>
<ns1:PayGateId> {{PayGateId}} </ns1:PayGateId>
<ns1:Password> {{Password}} </ns1:Password>
</ns1:Account>
<ns1:PayRequestId> {{PayRequestId}} </ns1:PayRequestId>
</ns1:QueryRequest>
</ns1:SingleFollowUpRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
解决方案
一般来说,您可以将大多数 json/xml 对象插入在线转换工具中,为您生成一些 C#。然后您可以使用普通的 c# 类而不是像现在这样进行字符串操作。例如:。
/*
Licensed under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="Account", Namespace="http://www.paygate.co.za/PayHOST")]
public class Account {
[XmlElement(ElementName="PayGateId", Namespace="http://www.paygate.co.za/PayHOST")]
public string PayGateId { get; set; }
[XmlElement(ElementName="Password", Namespace="http://www.paygate.co.za/PayHOST")]
public string Password { get; set; }
}
[XmlRoot(ElementName="QueryRequest", Namespace="http://www.paygate.co.za/PayHOST")]
public class QueryRequest {
[XmlElement(ElementName="Account", Namespace="http://www.paygate.co.za/PayHOST")]
public Account Account { get; set; }
[XmlElement(ElementName="PayRequestId", Namespace="http://www.paygate.co.za/PayHOST")]
public string PayRequestId { get; set; }
}
[XmlRoot(ElementName="SingleFollowUpRequest", Namespace="http://www.paygate.co.za/PayHOST")]
public class SingleFollowUpRequest {
[XmlElement(ElementName="QueryRequest", Namespace="http://www.paygate.co.za/PayHOST")]
public QueryRequest QueryRequest { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope")]
public class Body {
[XmlElement(ElementName="SingleFollowUpRequest", Namespace="http://www.paygate.co.za/PayHOST")]
public SingleFollowUpRequest SingleFollowUpRequest { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="SOAP-ENV", Namespace="http://www.w3.org/2000/xmlns/")]
public string SOAPENV { get; set; }
[XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns1 { get; set; }
}
}
https://xmltocsharp.azurewebsites.net/
我只是随机选择了一个站点。对于 json,我通常选择 quicktype,但它不支持 xml only json。
推荐阅读
- facebook - 共享到我管理的 Facebook 页面的内容仅对我可见
- javafx - JavaFX:setDisable(true) DatePicker 问题
- c# - Xamarin - 选择器所选项目未更改
- python - #Python 遍历具有空 (nan) 值的数组
- javascript - 如何将 document.getElementById 转换为 jquery?
- assembly - BGE 指令 ARM
- javascript - 如何将生成的运行时块捆绑到 Webpack 4 中的显式供应商块中
- android - Safe Args:使用 parcelables 列表
- html - 使用 ::first-letter 将字符串中的第一个字母大写。漏洞?
- javascript - Markercluster spiderfy - 附近的坐标以单个 spiderfy 显示,而不是单独的标记