要使用web服务,我需要在soapEnvelope中插入一个子节点,作为Body节点的第一个子节点。在这个节点中,我想要导入一个Xml文档。但是,它失败了,无论我做什么,它都将它作为与导入并行的兄弟节点。我做错了什么?这里我的代码(importeddoc是一个传入的xml文档)
XmlDocument soapEnvelope = new XmlDocument();
soapEnvelope.LoadXml("<soapenv:Envelope xmlns:soapenv =
"http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
</soapenv:Envelope>");
XmlNode headernode = soapEnvelope.CreateNode("element",
"soapenv:Header", "http://schemas.xmlsoap.org/soap/envelope/");
XmlNode bodynode = soapEnvelope.CreateNode("element",
"soapenv:Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlElement root = soapEnvelope.DocumentElement;
root.AppendChild(headernode);
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(importeddoc.NameTable);
nsmgr.AddNamespace("myextranodens", "http://....");
XmlNode containernode =
importeddoc.SelectSingleNode("/myimportdata", nsmgr);
XmlNode importNode = soapEnvelope.ImportNode(containernode,
true);
XmlNode myextranode =
bodynode.AppendChild(soapEnvelope.CreateElement("myextranodename"));
XmlNode final = myextranode.AppendChild(importNode);
bodynode.AppendChild(final);
root.AppendChild(bodynode);
return soapEnvelope;
I want it so:
<soapenv:Body>
<myextranodename>
<myimportdata>
</myextranodename>
</soapenv:Body>
. .但现在是:
<myextranodename></myextranodename>
<myimportdata>
我做错了什么?谢谢你的帮助。
啊!我必须删除最后一个bodynode追加子节点:
bodynode.AppendChild(final);