locked
Linq to xml to create soap documents not working as expected RRS feed

  • Question

  • User-1903090650 posted

    After looking at this site Create document with xml namespace

    I am trying to create a soap envelope that would look something like this

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:b="http://foo.com" xmlns:c="http://bar.com">
    	<SOAP-ENV:Header/>
    	<soap:Body>
    		<b:AvailabilityRequest xmlns="http://foo.com" xmlns:b="http://foo.com">
    			<b:Availabilities>
    				<b:Availability blockTime="false">
    					<b:TemplateIDs>
    						<c:UniqueID source="ABC">15</c:UniqueID>
    					</b:TemplateIDs>															
                                                 <b:TimeSpans>
    						<c:TimeSpan>
    							<c:Start>2018-06-01T08:00:00Z</c:Start>
    							<c:End>2018-07-01T22:00:00Z</c:End>
    						</c:TimeSpan>
    					</b:TimeSpans>					
    				</b:Availability>
    			</b:Availabilities>
    		</b:AvailabilityRequest>
    	</soap:Body>
    </soap:Envelope>

    This is my current code (part of it):

    XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
                XNamespace b = "http://foo.com";
                XNamespace c = "http://bar.com";
    
                var xDoc = new XDocument(
                    new XDeclaration("1.0", "utf-8", String.Empty),
                        new XElement(soap + "Envelope",
                            new XAttribute(XNamespace.Xmlns+"soap", soap.NamespaceName),
                            new XAttribute(XNamespace.Xmlns + "SOAP-ENV", soap.NamespaceName),
                            new XAttribute(XNamespace.Xmlns +"b", b.NamespaceName),
                            new XAttribute(XNamespace.Xmlns+"c", c.NamespaceName),
                            new XElement(soap+"Header"),
                            new XElement(soap+"Body",
                                new XElement(b+"AvailabilityRequest",
                                    new XAttribute("xmlns", "http://foo.com"),
                                    
                                    new XElement(b+"Availabilities"
                                        //new XElement(b + "Availability", new XAttribute("blockTime", "false"),
                                        //    new XElement(b + "TemplateIDs",
                                        //        new XElement(c + "UniqueID", 
                                        //    new XElement(b + "TimeSpans",
                                        //        new XElement(c + "Timespan",
                                        //            new XElement(c + "Start", DateTime.UtcNow.ToString("o")),
                                        //            new XElement(c + "End", DateTime.Now.AddDays(1).ToString("o")))))
                                        )
                                        )
                                )));
                Console.WriteLine(xDoc);

    However the output I get is 

    <SOAP-ENV:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:b="http://foo.com" xmlns:c="http://bar.com">
      <SOAP-ENV:Header />
      <SOAP-ENV:Body>
        <AvailabilityRequest xmlns="http://foo.com">
          <Availabilities />
        </AvailabilityRequest>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    Inside body I was expecting <b:AvailabilityRequest xmlns="http://foo.com" xmlns:b="http://foo.com">

    Any tips on how I should change my code to get desired output?

    Monday, March 5, 2018 10:34 AM