提取Xdocument soap响应体到新的Xdocument中

本文关键字:Xdocument soap 响应 提取 | 更新日期: 2023-09-27 18:07:33

我有一个XML,它被解析成一个XDocument:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <MyResponse xmlns="https://www.domain.net/">
      <Node1>0</Node1>
    </MyResponse>
  </soap:Body>
</soap:Envelope>

基本上我要创建一个新的XDocument它的根是

<MyResponse xmlns="https://www.domain.net/">
      <Node1>0</Node1>
    </MyResponse>

所以本质上我试图从肥皂体中提取这个。我试过用Linq解析这个,但似乎不能用这个新的根返回一个新的XDocument。什么好主意吗?

Thanks in advance

提取Xdocument soap响应体到新的Xdocument中

我想这个会成功的:

using System;
using System.Xml.Linq;
namespace SO39545160
{
  class Program
  {
    static string xmlSource = "<soap:Envelope xmlns:soap='"http://schemas.xmlsoap.org/soap/envelope/'" xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" xmlns:xsd='"http://www.w3.org/2001/XMLSchema'">" +
      "<soap:Body>" +
      "<MyResponse xmlns = '"https://www.domain.net/'" >" +
      "<Node1 > 0 </Node1 >" +
      "</MyResponse>" +
      "</soap:Body>" +
      "</soap:Envelope>";
    static void Main(string[] args)
    {
      XDocument xdoc = XDocument.Parse(xmlSource);
      var subXml = xdoc.Document.Elements(XName.Get(@"{http://schemas.xmlsoap.org/soap/envelope/}Envelope")).Elements(XName.Get(@"{http://schemas.xmlsoap.org/soap/envelope/}Body")).Elements(XName.Get(@"{https://www.domain.net/}MyResponse"));
      foreach (var node in subXml)
      {
        XDocument myRespDoc = new XDocument(node);
        Console.WriteLine(myRespDoc);
      }
      Console.WriteLine();
      Console.WriteLine("END");
      Console.ReadLine();
    }
  }
}