修改名称空间前缀Web Services C#

时间:2015-06-05 12:14:41

标签: c# xml web-services soap

我们正在接收SOAP请求,但我们无法对其进行全部修改。所以现在我们必须改变我们的web服务以适应呼叫。我几乎在那里,期望他们的命名空间的前缀与生成的前缀不同,并且除非它相同,否则它不会触及该方法:

这是我的Web服务:

[WebService(Namespace = "http://domain.co.za/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]
public class MyWebservice : System.Web.Services.WebService
{   
    [WebMethod]        
    public string PushMessage(object payload)
    {
    }
}

他们发送的SOAP:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://domain.co.za/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
    <ns0:username soapenv:actor=""></ns0:username>
    <ns0:password soapenv:actor=""></ns0:password>
</soapenv:Header>
<soapenv:Body>
    <ns0:PushMessage>
    <type>confirmation</type>
    <autoRelease>true</autoRelease>
        <payload>
            <Confirmation>
                <MessageEnvelope>
                .
                .
                </MessageEnvelope>
            </Confirmation>
        </payload>
    </ns0:PushMessage>
</soapenv:Body>
</soapenv:Envelope>

正如您所看到的那样,他们正在使用ns0,WDSL生成的那个是web,无论如何都要强制我的命名空间生成为ns0:而不是默认的web:

有没有办法从有效负载元素中删除ns0 / web?它们不是使用有效负载发送命名空间,而是WDSL将其生成为web:payload

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试正则表达式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace Calendar
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://domain.co.za/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
              "<soapenv:Header>" +
                "<ns0:username soapenv:actor=\"\">            </ns0:username>" +
                "<ns0:password soapenv:actor=\"\">            </ns0:password>" +
              "</soapenv:Header>" +
              "<soapenv:Body>" +
                "<ns0:PushMessage>" +
                  "<type>confirmation            </type>" +
            "<autoRelease>true            </autoRelease>" +
                  "<payload>" +
                    "<Confirmation>" +
                      "<MessageEnvelope>" +
                      "</MessageEnvelope>" +
                    "</Confirmation>" +
                  "</payload>" +
                "</ns0:PushMessage>" +
              "</soapenv:Body>" +
            "</soapenv:Envelope>";

            string pattern = "(</?)(\\w:)(\\w)";
            Regex expr = new Regex(pattern);
            input = expr.Replace(input, "$1$3");
            XDocument doc = XDocument.Parse(input);
        }
    }



}