将Soap Headers添加到XDocument

时间:2014-09-10 11:57:45

标签: vb.net soap linq-to-xml soapheader

我正在创建一个具有以下结构的XDocument:

 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))
                                   ))

要获得以下输出:

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

我想添加以下标题

    <?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="">

我应该将它们添加为新XDeclaration,新XElement吗? 是什么类型的肥皂标题?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

<soap:Envelope><soap:Body>显然是元素。你可以这样做,用soap header构建XML:

'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
                           )

输出:

<?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>