手动处理的肥皂发送 - XmlNode:NodeTypes和Attributes只读

时间:2011-05-27 21:00:19

标签: c# soap xmlnode

我必须调用webservice,但手动构建xml。我选择了System.Xml来做这件事,但这给我带来了很多痛苦。

我知道的每个SOAP都包含3个部分:

  • XmlDeclaration
  • 信封的标题或SOAP-ENV:Envelope
  • 的 信封或身体 SOAP-ENV:身体

事情是我的XmlDocument只能包含一个根元素。 (不知道为什么) 如果我做

MyXMLDoc.CreateNode(XmlNodeType.DocumentFragment, "SOAP-ENV", "Envelope", "http://tempuri.org");

它不允许我追加属性。它说那些是只读的。

RESUMING:

如何编写 this 但没有root和XmlDeclaration?以及如何调用web服务(使用此xml)?

-<root>
  <SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" /> 
- <SOAP-ENV:Body>
- <ZbcInsertRequest xmlns:ns1="'urn:sap-com:document:sap:soap:functions:mc-style'">
  <Destino>qas</Destino> 
- <IClrQst>
  <Request>foo</Request> 
  <Proyecto>foofoo</Proyecto> 
  <Modulocq /> 
  <Titureq>foofoofoo</Titureq> 
  <Tiporeq /> 
  <Prioridad>0</Prioridad> 
  <Clasific /> 
  <Asignadoa>foofoofoofoo</Asignadoa> 
  <Destino>qas</Destino> 
  <Solicita>foo</Solicita> 
  <Autoriza>c.foo</Autoriza> 
  <ModTransp /> 
  <ReqSox>F</ReqSox> 
  <Notfylst /> 
  <Tipodeploy>Normal</Tipodeploy> 
  <Paqdeploy>CDVQA00000000</Paqdeploy> 
  </IClrQst>
- <TRequest>
- <item>
  <Trkorr>DESK9A0ZJT</Trkorr> 
  <Secuencia>0</Secuencia> 
  <Solicita>c.foo</Solicita> 
  <Type /> 
  <Id /> 
  <Numero /> 
  <Message /> 
  </item>
  </TRequest>
  </ZbcInsertRequest>
  </SOAP-ENV:Body>
  </root>

这是我正在连接到

的wsdl
<wsdl:definitions targetNamespace="urn:sap-com:document:sap:soap:functions:mc-style">
<wsdl:types>
<xsd:schema targetNamespace="urn:sap-com:document:sap:rfc:functions" elementFormDefault="unqualified" attributeFormDefault="qualified">
<xsd:simpleType name="char1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char10">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char100">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="100"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char15">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="15"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char20">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char220">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="220"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char255">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char30">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char32">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="32"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char50">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numeric3">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="3"/>
<xsd:pattern value="\d*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numeric6">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="6"/>
<xsd:pattern value="\d*"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
<xsd:schema targetNamespace="urn:sap-com:document:sap:soap:functions:mc-style" elementFormDefault="unqualified" attributeFormDefault="qualified">
<xsd:import namespace="urn:sap-com:document:sap:rfc:functions"/>
<xsd:simpleType name="char1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char10">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char12">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="12"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char120">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="120"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char20">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char250">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="250"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char3">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="3"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char4">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="4"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="char60">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="60"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="date">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
<xsd:pattern value="\d\d\d\d-\d\d-\d\d"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numeric5">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="5"/>
<xsd:pattern value="\d*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numeric6">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="6"/>
<xsd:pattern value="\d*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="time">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="8"/>
<xsd:pattern value="\d\d:\d\d:\d\d"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="ZtyEE070">
<xsd:sequence>
<xsd:element name="Trkorr" type="tns:char20"/>
<xsd:element name="As4text" type="tns:char60"/>
<xsd:element name="Trfunction" type="n0:char1"/>
<xsd:element name="Trstatus" type="n0:char1"/>
<xsd:element name="Tarsystem" type="tns:char10"/>
<xsd:element name="Korrdev" type="tns:char4"/>
<xsd:element name="As4user" type="tns:char12"/>
<xsd:element name="As4date" type="tns:date"/>
<xsd:element name="As4time" type="tns:time"/>
<xsd:element name="Strkorr" type="tns:char20"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ZtyEE071">
<xsd:sequence>
<xsd:element name="Trkorr" type="n0:char20"/>
<xsd:element name="As4pos" type="tns:numeric6"/>
<xsd:element name="Pgmid" type="tns:char4"/>
<xsd:element name="Object" type="tns:char4"/>
<xsd:element name="Objname" type="tns:char120"/>
<xsd:element name="Objfunc" type="tns:char1"/>
<xsd:element name="Lockflag" type="tns:char1"/>
<xsd:element name="Gennum" type="tns:char3"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Zclrqstdat">
<xsd:sequence>
<xsd:element name="Request" type="n0:char15"/>
<xsd:element name="Proyecto" type="n0:char50"/>
<xsd:element name="Modulocq" type="tns:char250"/>
<xsd:element name="Titureq" type="tns:char250"/>
<xsd:element name="Tiporeq" type="n0:char30"/>
<xsd:element name="Prioridad" type="n0:char15"/>
<xsd:element name="Clasific" type="n0:char100"/>
<xsd:element name="Asignadoa" type="n0:char50"/>
<xsd:element name="Destino" type="n0:char10"/>
<xsd:element name="Solicita" type="n0:char50"/>
<xsd:element name="Autoriza" type="n0:char50"/>
<xsd:element name="Modtransp" type="n0:char1"/>
<xsd:element name="Reqsox" type="n0:char1"/>
<xsd:element name="Notfylst" type="n0:char255"/>
<xsd:element name="Tipodeploy" type="n0:char50"/>
<xsd:element name="Paqdeploy" type="n0:char15"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Zrequest">
<xsd:sequence>
<xsd:element name="Trkorr" type="n0:char20"/>
<xsd:element name="Secuencia" type="tns:numeric5"/>
<xsd:element name="Solicita" type="n0:char50"/>
<xsd:element name="Type" type="n0:char1"/>
<xsd:element name="Id" type="n0:char20"/>
<xsd:element name="Numero" type="n0:numeric3"/>
<xsd:element name="Message" type="n0:char220"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Bapiret2">
<xsd:sequence>
<xsd:element name="Type" type="n0:char1"/>
<xsd:element name="Id" type="n0:char20"/>
<xsd:element name="Number" type="n0:numeric3"/>
<xsd:element name="Message" type="n0:char220"/>
<xsd:element name="Logno" type="n0:char20"/>
<xsd:element name="Logmsgno" type="n0:numeric6"/>
<xsd:element name="Messagev1" type="n0:char50"/>
<xsd:element name="Messagev2" type="n0:char50"/>
<xsd:element name="Messagev3" type="n0:char50"/>
<xsd:element name="Messagev4" type="n0:char50"/>
<xsd:element name="Parameter" type="n0:char32"/>
<xsd:element name="Row" type="xsd:int"/>
<xsd:element name="Field" type="n0:char30"/>
<xsd:element name="System" type="n0:char10"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="TableOfztyEE070">
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded" type="tns:ZtyEE070"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="TableOfztyEE071">
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded" type="tns:ZtyEE071"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="TableOfzrequest">
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded" type="tns:Zrequest"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ZbcInsertRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Destino" type="n0:char32"/>
<xsd:element name="EtRequestHeader" minOccurs="0" type="tns:TableOfztyEE070"/>
<xsd:element name="EtRequestObj" minOccurs="0" type="tns:TableOfztyEE071"/>
<xsd:element name="IClrqst" type="tns:Zclrqstdat"/>
<xsd:element name="TRequest" type="tns:TableOfzrequest"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ZbcInsertRequestResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EtRequestHeader" minOccurs="0" type="tns:TableOfztyEE070"/>
<xsd:element name="EtRequestObj" minOccurs="0" type="tns:TableOfztyEE071"/>
<xsd:element name="Return" type="tns:Bapiret2"/>
<xsd:element name="TRequest" type="tns:TableOfzrequest"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ZbcInsertRequestString">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EtRequestHeader" minOccurs="0" type="tns:TableOfztyEE070"/>
<xsd:element name="EtRequestObj" minOccurs="0" type="tns:TableOfztyEE071"/>
<xsd:element name="Sdestino" type="xsd:string"/>
<xsd:element name="SiClrqst" type="xsd:string"/>
<xsd:element name="TRequest" type="tns:TableOfzrequest"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ZbcInsertRequestStringResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EtRequestHeader" type="tns:TableOfztyEE070"/>
<xsd:element name="EtRequestObj" type="tns:TableOfztyEE071"/>
<xsd:element name="Return" type="tns:Bapiret2"/>
<xsd:element name="TRequest" type="tns:TableOfzrequest"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<style/>
<wsdl:message name="ZbcInsertRequest">
<wsdl:part name="parameters" element="tns:ZbcInsertRequest"/>
</wsdl:message>
<wsdl:message name="ZbcInsertRequestResponse">
<wsdl:part name="parameters" element="tns:ZbcInsertRequestResponse"/>
</wsdl:message>
<wsdl:message name="ZbcInsertRequestString">
<wsdl:part name="parameters" element="tns:ZbcInsertRequestString"/>
</wsdl:message>
<wsdl:message name="ZbcInsertRequestStringResponse">
<wsdl:part name="parameters" element="tns:ZbcInsertRequestStringResponse"/>
</wsdl:message>
<sap:Feature name="design_0 " uri="http://www.sap.com/webas/630/soap/features/authentication/">
<sap:Property qname="n1:AuthenticationLevel">
<sap:Option value="n1:None"/>
</sap:Property>
</sap:Feature>
<wsdl:portType name="ZICQ">
<sap:useFeature feature="tns:design_0 "/>
<wsdl:operation name="ZbcInsertRequest">
<wsdl:input message="tns:ZbcInsertRequest"/>
<wsdl:output message="tns:ZbcInsertRequestResponse"/>
</wsdl:operation>
<wsdl:operation name="ZbcInsertRequestString">
<wsdl:input message="tns:ZbcInsertRequestString"/>
<wsdl:output message="tns:ZbcInsertRequestStringResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ZICQSoapBinding" type="tns:ZICQ">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ZbcInsertRequest">
<soap:operation soapAction=""/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="ZbcInsertRequestString">
<soap:operation soapAction=""/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ZICQService">
<wsdl:port name="ZICQSoapBinding" binding="tns:ZICQSoapBinding">
<soap:address location="http://hmxwasd1.hylsamex.com.mx:8000/sap/bc/srt/rfc/sap/ZICQ?sap-client=100"/>
</wsdl:port>

1 个答案:

答案 0 :(得分:0)

针对您的具体问题:在SOAP信封中,Body元素必须是Envelope的子元素。你不能让信封成为一个空元素,然后是它的身体。下面的代码显示了如何在XmlDocument对象中加载此文档的修改版本。

    public class StackOverflow_6157636_751090
    {
        public static void Test()
        {
            string xml = @"  <SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xs=""http://www.w3.org/2001/XMLSchema""> 
 <SOAP-ENV:Body>
 <ZbcInsertRequest xmlns:ns1=""urn:sap-com:document:sap:soap:functions:mc-style"">
  <Destino>qas</Destino> 
 <IClrQst>
  <Request>foo</Request> 
  <Proyecto>foofoo</Proyecto> 
  <Modulocq /> 
  <Titureq>foofoofoo</Titureq> 
  <Tiporeq /> 
  <Prioridad>0</Prioridad> 
  <Clasific /> 
  <Asignadoa>foofoofoofoo</Asignadoa> 
  <Destino>qas</Destino> 
  <Solicita>foo</Solicita> 
  <Autoriza>c.foo</Autoriza> 
  <ModTransp /> 
  <ReqSox>F</ReqSox> 
  <Notfylst /> 
  <Tipodeploy>Normal</Tipodeploy> 
  <Paqdeploy>CDVQA00000000</Paqdeploy> 
  </IClrQst>
 <TRequest>
 <item>
  <Trkorr>DESK9A0ZJT</Trkorr> 
  <Secuencia>0</Secuencia> 
  <Solicita>c.foo</Solicita> 
  <Type /> 
  <Id /> 
  <Numero /> 
  <Message /> 
  </item>
  </TRequest>
  </ZbcInsertRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            MemoryStream ms = new MemoryStream();
            Encoding encoding = new UTF8Encoding(false);
            XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = false, Encoding = encoding, IndentChars = "  " });
            doc.Save(w);
            w.Flush();
            Console.WriteLine(encoding.GetString(ms.ToArray()));
        }
    }

现在,您可以考虑的另一个选项(如果可能)不是“手动”发送SOAP请求,而是使用某些工具(例如WCF或其他一些SOAP库)来创建可用于发送请求的代理服务。这样,您的客户端可以简单地“调用方法”,堆栈将负责为您创建SOAP信封。