简体   繁体   中英

Add Soap Headers to XDocument

I am creating an XDocument with the following structure:

 Dim xDocHandle As XDocument =
                                   New XDocument(
                                   New XDeclaration("1.0", Nothing, Nothing),
                                   New XElement("Element",
                                    New XElement("Dialogue",
                                    New XElement("Desc", AppDesc),
                                    New XElement("Num", Num),
                                    New XElement("Ref", Ref),
                                    New XElement("ms", Ms),
                                    New XElement("im", Im))
                                   ))

To have the following output:

<Element>
  <Dialogue>
    <Desc>test</Desc>
    <Num>1</Num>
    <Ref></Ref>
    <ms>2411616</ms>
    <im></im>
  </Dialogue>
</Element>

I want to add the following headers

    <?xml version="1.0"?>
    <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
    soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns="">

Should I add them as new XDeclaration , new XElement ? Whats the type of the soap headers?

Any help would be appreciated.

<soap:Envelope> and <soap:Body> are clearly elements. You can do something like this to construct XML with soap header :

'create <Element> node :'
Dim element As XElement = New XElement("Element",
                                    New XElement("Dialogue",
                                    New XElement("Desc", AppDesc),
                                    New XElement("Num", Num),
                                    New XElement("Ref", Ref),
                                    New XElement("ms", Ms),
                                    New XElement("im", Im))
                                   )
'create <soap:Envelope> node and add <Element> as child of <soap:Body> :'
Dim soap As XNamespace = "http://www.w3.org/2001/12/soap-envelope"
Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
                                New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
                                New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
                                New XElement(soap + "Body", element))
'create XDocument and set <soap:Envelope> as content'
Dim xDocHandle As XDocument =
                           New XDocument(
                           New XDeclaration("1.0", Nothing, Nothing),
                            soapEnvelope
                           )

Output :

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
               soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
    <soap:Body>
        <Element>
          <Dialogue>
            <Desc>test</Desc>
            <Num>1</Num>
            <Ref></Ref>
            <ms>2411616</ms>
            <im></im>
          </Dialogue>
        </Element>
    </soap:Body>
</soap:Envelope>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM