2016-09-17 58 views
0

我有被解析到一個XDocument XML:抽取的XDocument SOAP響應體內轉化爲新的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> 

基本上我想創建有其作爲

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

所以根新的XDocument本質上我試圖從肥皂身體中提取這個。我試着用Linq解析這個,但似乎無法返回一個新的XDocument與這個新的根。有任何想法嗎?

在此先感謝

+0

'新的XDocument(old.Element(「{} http://schemas.xmlsoap.org/soap/envelope/ Envelope「).Element(」{http://schemas.xmlsoap.org/soap/envelope/}Body「).Element(」{https://www.domain.net/}MyResponse「))' – PetSerAl

回答

-1

我認爲這將這樣的伎倆:

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(); 
    } 
    } 
} 
+0

返回零結果; – CR41G14

+0

@ CR41G14:當我運行它時,它將打印到foreach循環中的控制檯。也許xmlSource字符串在複製時損壞了? –

+0

這工作var newDoc = XDocument(response.Document.Descendants(XName.Get(「MyResponse」,「https://www.domain.net/」)));感謝您的指導! – CR41G14