首页 > 解决方案 > .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>

标签: c#xml

解决方案


一般来说,您可以将大多数 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。


推荐阅读