如何从api代理类对象生成正确的soapenvelop包装器? [英] How do I generate a proper soapenvelop wrapper from api proxy class object?

查看:129
本文介绍了如何从api代理类对象生成正确的soapenvelop包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从soapui复制的。但是,当我通过代码生成如何添加肥皂包装封面,标题和正文标签?



我尝试过:



This one copied from soapui. But when i generate through code how can add the soap envelop wrapper,header and body tags?

What I have tried:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:whol="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd" 
xmlns:stan="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">
<soapenv:Header/>
<soapenv:Body>
	<GetRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<AccessKey xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">MXANJVAUCAJASPH1</AccessKey>
	<ProductID xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">FIBRE</ProductID>
	<Scope xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">QUALIFY</Scope>
	<Parameters xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">
	<Param id="DirectoryID" xmlns="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">LOC100056097744</Param>
	<Param id="CVCID" xmlns="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">AggCVC000000015</Param>
	</Parameters>
	</GetRequest>
</soapenv:Body>
</soapenv:Envelope>

推荐答案

我的建议是查看序列化。对我来说,这是最简单的路线,但你也可以做一些像Linq to XML或使用XML Writer ......但这会产生很多代码。



第一步要做的就是拿肥皂/ xml,转到 Xml2CSharp.com |将XML示例转换为可与XmlSerializer兼容的C#类 [ ^ ]



并生成代表你肥皂信封的类。



然后,使用生成的类,你可以创建表示信封部分的对象,并将值分配给代码中的任何其他变量。



My suggestion would be to look into serialization. To me, this is the easiest route to go but you could also do something like Linq to XML or use XML Writer...but that gets into a lot of code.

The firs thing to do is take your soap/xml, go to Xml2CSharp.com | Convert your XML Examples into XmlSerializer compatable C# Classes[^]

and generate the classes that represent your soap envelope.

Then, using the classes that get generated, you can create objects that represent portions of your envelope and assign the values as you would to any other variable in code.

var env = new Envelope();
            env.Body = new Body();
            env.Body.GetRequest = new GetRequest();
            env.Body.GetRequest.AccessKey = new AccessKey();
            env.Body.GetRequest.Parameters  = new Parameters();
            env.Body.GetRequest.Parameters.Param = new List<Param>();
            env.Body.GetRequest.ProductID =  new ProductID();
            env.Body.GetRequest.Scope = new Scope();

            env.Body.GetRequest.Xsi = "http://www.w3.org/2001/XMLSchema-instance";
            env.Body.GetRequest.Xsd = "http://www.w3.org/2001/XMLSchema";

            env.Body.GetRequest.AccessKey.Text = "MXANJVAUCAJASPH1";
            env.Body.GetRequest.ProductID.Text = "FIBRE";
            env.Body.GetRequest.Scope.Text = "QUALIFY";


            env.Body.GetRequest.Parameters.Param.Add(new Param()
            {
                Id = "DirectoryID",
                Text = "LOC100056097744",
                Xmlns = ""
            });

            env.Body.GetRequest.Parameters.Param.Add(new Param()
            {
                Id = "CVCID",
                Text = "AggCVC000000015",
                Xmlns = ""
            });



            var xmlString = string.Empty;
            XmlSerializer xmlSerializer = new XmlSerializer(env.GetType());

            using(StringWriter textWriter = new StringWriter())
            {
                xmlSerializer.Serialize(textWriter, env);
                xmlString = textWriter.ToString();
            }

            Console.WriteLine(xmlString);





我不包括课程信封,因为你可以从xml2csharp网站获取,并将其复制/粘贴到你的应用程序。



但上面生成以下XML序列化时





I'm not including the classes for the envelope as you can get those from xml2csharp website and copy/paste it into your app.

But the above generates the following XML when it is serialized

<?xml version="1.0" encoding="utf-16"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Body>
    <GetRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <AccessKey xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">MXANJVAUCAJASPH1</AccessKey>
      <ProductID xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">FIBRE</ProductID>
      <Scope xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">QUALIFY</Scope>
      <Parameters xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">
        <Param id="DirectoryID" xmlns="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">LOC100056097744</Param>
        <Param id="CVCID" xmlns="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">AggCVC000000015</Param>
      </Parameters>
    </GetRequest>
  </Body>
</Envelope>


这篇关于如何从api代理类对象生成正确的soapenvelop包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆