Добавить заголовки мыла в XDocument

Я создаю 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 ответ

Решение

<soap:Envelope> а также <soap:Body> явно элементы. Вы можете сделать что-то вроде этого, чтобы создать 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>
Другие вопросы по тегам