添加SOAP头的XDocument
问题描述:
我创建具有以下结构的一个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
? 什么是肥皂头的类型?
任何帮助,将不胜感激。
答
<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>
我想补充
一个xmlns属性,但是当我这样做,@HelpASisterOut我建议打开新的问题.. – har07 2014-09-30 07:17:22