2014-09-10 92 views
1

我創建具有以下結構的一個XDocument:添加SOAP頭的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? 什麼是肥皂頭的類型?

任何幫助,將不勝感激。

回答

2

<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> 
+0

我想補充一個xmlns屬性,但是當我這樣做,自動獲得xmlns屬性以及 – HelpASisterOut 2014-09-30 07:12:44

+0

@HelpASisterOut我建議打開新的問題.. – har07 2014-09-30 07:17:22