簡體   English   中英

Append 兩個 XMlDocument C#

[英]Append two XMlDocument C#

My soap service receives a XML and i need to add soap enevelope to the xml before i send it to a HTTP service, i get a error at line httpDocumet.DocumentElement.AppendChild(xml.DocumentElement);

我如何將 append soap 標頭連接到我的 xml?

    [WebMethod]
    public XmlDocument HelloWorld(XmlDocument xml)
    {

        XmlDocument httpDocumet = new XmlDocument();
        XmlElement soapEnvelope = httpDocumet.CreateElement("soap", "Header", "http://schemas.xmlsoap.org/soap/envelope/");
        soapEnvelope.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        soapEnvelope.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        httpDocumet.AppendChild(soapEnvelope);
        XmlElement soapBody =httpDocumet.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
        httpDocumet.DocumentElement.AppendChild(soapBody);
        httpDocumet.DocumentElement.AppendChild(xml.DocumentElement);

錯誤信息

System.Web.Services.Protocols.SoapException:服務器無法處理請求。 ---> System.ArgumentException:要插入的節點來自不同的文檔上下文。 在 System.Xml.XmlNode.AppendChild(XmlNode newChild) 在 WebService1.Service1.HelloWorld(XmlDocument xml)

您需要先ImportNode ,然后是AppendChild

var root = httpDocument.ImportNode(xml.DocumentElement, true);
httpDocument.DocumentElement.AppendChild(root);

請參閱http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.importnode.aspx

為什么不將 header 添加到xml變量並返回呢?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM