XML Serialization and SOAP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    XML Serialization and SOAP

    I created a piece of code with which I can create an XML which look like this:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <test xmlns:test="http://www.test.nl">
      <a>A</a>
      <b>
        <c>1</c>
        <c>2</c>
        <c>4</c>
        <c>3</c>
      </b>
    </test>
    I did this creating a class using XSD.exe on the following XSD:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="test">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="a" type="xs:string" />
            <xs:element name="b">
              <xs:complexType>
                <xs:sequence>
                  <xs:element maxOccurs="unbounded" name="c" type="xs:string" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>

    My question:
    If I want to send this xml as a request to a SOAP server, how do I add the SOAP-envelope?

    I tried to do this:
    1) define a class for the SOAP envelope
    Code:
        [XmlInclude(typeof(SOAP))]
        [Serializable]
        public class SOAP
        {
            public string Head { get; set; }
            public object Body { get; set; }
        }
    When I try to insert my XML thing into this like this:
    Code:
    SOAP x3 = new SOAP { Head = "x1", Body = x2 };
                XmlSerializer xs3 = new XmlSerializer(x3.GetType(), "");
                XmlWriterSettings xws3 = new XmlWriterSettings();
                xws3.Indent = true;
                xws3.Encoding = Encoding.UTF8;
                XmlWriter xw3 = XmlWriter.Create("test3.xml", xws3);
                xs3.Serialize(xw3, x3, namespaces);
    It fails with an 'InvalidOperati onException' on the xs3.Serialize.
    ('x2' is the object holding the XML shown earlier...)
    Last edited by Luuk; Apr 24 '16, 05:50 PM. Reason: small changes in code...
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #2
    Could you please paste the contents of here X3 just before the Serialize sentence.

    Comment

    • Luuk
      Recognized Expert Top Contributor
      • Mar 2012
      • 1043

      #3
      I made some changes to above code (I will edit my original post too)

      the contents of X3 is (copied from 'Immediate Window':
      Code:
      x3
      {CreateXML_1.SOAP}
          Body: {test}
          Head: "x1"
      x3.Body
      {test}
          a: "A"
          aField: "A"
          b: {string[0x00000004]}
          bField: {string[0x00000004]}
      x3.Head
      "x1"
      If you was hoping to see something else please give directions on how to get that info... ;)


      BTW: If I change the definition of 'Body' (which is currently an 'object' to 'test' than I do no get the error. But that's not what I want, because I need the possibility to send other XML's to the same server.

      After changing 'object' to 'test', i'm getting this test3.xml:
      Code:
      <?xml version="1.0" encoding="utf-8"?>
      <SOAP xmlns:test="http://www.test.nl">
        <Head>x1</Head>
        <Body>
          <a>A</a>
          <b>
            <c>1</c>
            <c>2</c>
            <c>4</c>
            <c>3</c>
          </b>
        </Body>
      </SOAP>
      I'm loosing the 'test'-rootTag, which I also do not like ;)

      Comment

      • madankarmukta
        Contributor
        • Apr 2008
        • 308

        #4
        If I understood your question correctly, there might be some problem the format for the Header contents. Probably you could look into http://stackoverflow.com/questions/2...nvelope-in-net

        Hope this helps.

        Comment

        • Luuk
          Recognized Expert Top Contributor
          • Mar 2012
          • 1043

          #5
          If you are referring to the answer "I was able to solve this by using an XLST to wrap the XML in soap"...

          This is (I tihkn) a no go, because this would probable be tooo slow. (and to complex to implement (for /me ;)).

          But i'll give it a test tomorrow.... ;)

          Comment

          Working...