代码之家  ›  专栏  ›  技术社区  ›  jaywon

用XMLDocument VB.NET构建SOAP消息

  •  1
  • jaywon  · 技术社区  · 14 年前

    肥皂 消息使用 在VB.NET中(尽管答案很好)。

    我使用以下代码手动创建 消息的名称空间前缀 soap:Body 正在输出XML中剥离:

    Dim soapEnvelope As XmlElement = _xmlRequest.CreateElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
    soapEnvelope.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
    soapEnvelope.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    _xmlRequest.AppendChild(soapEnvelope)
    Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", String.Empty)
    _xmlRequest.DocumentElement.AppendChild(soapHeader)
    Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", String.Empty)
    _xmlRequest.DocumentElement.AppendChild(soapBody)
    

        <?xml version="1.0" encoding="UTF-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <Header>
      ...
     </Header>
     <Body>
      ....
     </Body>
    </soap:Envelope>
    

    我需要的是:

    <?xml version="1.0" encoding="UTF-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <soap:Header>
      ...
     </soap:Header>
     <soap:Body>
      ....
     </soap:Body>
    </soap:Envelope>
    

    我很感激所有的意见,但不管是怎么说的 如果在接收端工作或被解析,或者诸如此类的东西,那么底线是我需要生成所描述的XML。提前谢谢!

    解决方案: 非常类似于 夸特迈斯特 命名空间URI 文档元素 :

    Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", _xmlRequest.DocumentElement.NamespaceURI)
    Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", _xmlRequest.DocumentElement.NamespaceURI)
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   Quartermeister    14 年前

    您需要将Header和Body元素上的XML命名空间设置为soap命名空间:

    Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", "http://schemas.xmlsoap.org/soap/envelope/")
    Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/")