【问题标题】:Add Soap Headers to XDocument将肥皂标题添加到 XDocument
【发布时间】:2014-09-10 11:57:45
【问题描述】:

我正在创建一个具有以下结构的 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吗? 肥皂头的类型是什么?

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    &lt;soap:Envelope&gt;&lt;soap:Body&gt; 显然是元素。你可以做这样的事情来构造带有soap头的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>
    

    【讨论】:

    • 我想添加 一个 xmlns 属性,但是当我这样做时, 也会自动获取 xmlns 属性
    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    相关资源
    最近更新 更多