【问题标题】:Change XML Tag Prefix SOAP更改 XML 标记前缀 SOAP
【发布时间】:2019-04-17 08:49:51
【问题描述】:

我正在尝试创建带有前缀的 SOAP 消息。但是,我无法正确设置命名空间。我已经尝试了几天并尝试了许多我在网上找到的建议,但似乎没有一个有效。我希望你们中的一些人可以帮助我。 我得到的是

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Transaction xmlns="http://tempuri.org/">
      <bankingTransaction>
        <operation parameterOrder="string">
          <fault />
          <fault />
        </operation>
        <transactionDate>dateTime</transactionDate>
        <amount>int</amount>
      </bankingTransaction>
    </Transaction>
  </soap:Body>
</soap:Envelope>

&我真正需要的是

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <res:Transaction xmlns="res:http://tempuri.org/">
      <res:bankingTransaction>
        <res:operation parameterOrder="string">
          <res:fault />
          <res:fault />
        </res:operation>
        <res:transactionDate>dateTime</res:transactionDate>
        <res:amount>int</res:amount>
      </res:bankingTransaction>
    </res:Transaction>
  </soap:Body>
</soap:Envelope>

&我的MassageContact

[MessageContract]
public class BankingTransaction
{
   [MessageHeader] public Operation operation;
   [MessageHeader] public DateTime transactionDate;
   [MessageBodyMember] private unit sourceAccount;
   [MessageBodyMember] public int amount;
}

请帮我为我的 XML 元素添加前缀。 谢谢

【问题讨论】:

    标签: c# xml wcf xml-namespaces messagecontract


    【解决方案1】:

    你可能需要做这样的事情:

    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]
    [MessageContract]
    public class BankingTransaction
    {
       [MessageHeader] public Operation operation;
       [MessageHeader] public DateTime transactionDate;
       [MessageBodyMember] private unit sourceAccount;
       [MessageBodyMember] public int amount;
    }
    

    我不确定你是如何序列化你的对象的,但是像这样会添加前缀:

       XmlSerializerNamespaces x = new XmlSerializerNamespaces();
        x.Add("res", "http://tempuri.org/");
    

    可能将XmlSerializerNamespaces 添加到您的序列化过程中?很难说没有看到你在做什么。该命名空间中的所有合约/类可能都需要此属性:[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]

    【讨论】:

    • 我不知道我需要在哪里使用这个 XmlSerializerNamespaces x = new XmlSerializerNamespaces(); x.Add("res", "tempuri.org/");
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多