【问题标题】:Append two XMlDocument C#附加两个 XMlDocument C#
【发布时间】:2011-07-05 10:29:52
【问题描述】:

我的 soap 服务收到一个 XML,我需要先将 soap 信封添加到 xml,然后再将它发送到 HTTP 服务,我在 行 httpDocumet.DocumentElement.AppendChild(xml.DocumentElement);

如何在我的 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)

【问题讨论】:

    标签: c# .net asp.net web-services


    【解决方案1】:

    你需要先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

    【讨论】:

      【解决方案2】:

      您为什么不直接将标头添加到 xml 变量并返回它?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多