2016-09-17 4 views
0

나는하여 XDocument로 구문 분석 XML이 : 나는 그래서추출하여 XDocument 비누 응답 본문

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

같은 뿌리를 가진 새하여 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> 

본질적으로 저는 이것을 비누 몸체에서 추출하려고합니다. 내가 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

은 0을 반환합니다. 결과; – CR41G14

+0

@ CR41G14 : 실행하면 가 foreach 루프의 콘솔에 출력됩니다. 어쩌면 xmlSource 문자열을 복사 할 때 손상 되었습니까? –

+0

이 작업은 var newDoc = XDocument (response.Document.Descendants (XName.Get ("MyResponse", "https://www.domain.net/")))); 안내 주셔서 감사합니다! – CR41G14