2013-12-09 4 views
4

내가 xml을 얻을 때, 나는 그것을 특수 객체에 deserialize하고 웹 서비스 메소드의 매개 변수를 통해 전달해야한다.개체를 deserialize하는 비누 재료를 무시하는 방법?

코드 :

var document = new XmlDocument(); 
     document.Load(@"C:\Desktop\CteWebservice.xml"); 
var serializer = new XmlSerializer(typeof(OCTE)); 
var octe = (OCTE) serializer.Deserialize(new StringReader(document.OuterXml)); 

serviceClient.InsertOCTE(octe); 

하지만 역 직렬화 할 때 나는 봉투 태그와 다른 SOAP 물건을 무시해야 할

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" > was not expected. 

라는 오류가 발생합니다. 어떻게 할 수 있습니까?

편집 :

XML 파일 :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:ns="http://192.168.1.180:8085/"> 
<soapenv:Header/> 
<soapenv:Body> 
    <ns:INCLUIRCONHECIMENTOFRETESAIDA> 
    <ns:CONHECIMENTOFRETE> 
     <ns:CSTICMS></ns:CSTICMS> 
    </ns:CONHECIMENTOFRETE> 
    </ns:INCLUIRCONHECIMENTOFRETESAIDA> 
<soapenv:Body> 

시험 코드 :

XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/"; 
    XNamespace m = "http://192.168.1.180:8085/"; 
    var soapBody = xdoc.Descendants(soap + "Body").First().FirstNode; 

    var serializer = new XmlSerializer(typeof(OCTE)); 
    var responseObj = (OCTE)serializer.Deserialize(soapBody.CreateReader()); 

의 SOAPBody 내가 필요로하는 모든 정보로 가져옵니다. 하지만 내가 responseObj에 그것을 deserialize 할 때 나는 모든 값을 null로 얻는다.

+0

'XDocument'를 사용하고'document.Descendant ("OCTE")'를합니까? (또는 루트 이름이 무엇이든간에) –

+0

XmlDocument 클래스에서이 멤버를 찾을 수 있는지 여부를 나타내는 하위 항목입니다. – gog

+0

맞아, 나는'XDocument'와 혼동했다. 대신 그 수업을 사용할 수 있습니까? 어느 쪽이든, 나는 XML 트리를 네 몸의 가치로 이동시키는 방법이 있다고 확신한다. –

답변

6

을 찾고 있지만, W3C's example SOAP response를 사용하여 다음 코드와 무엇이다

var xdoc = XDocument.Load(@"C:\Desktop\CteWebservice.xml"); 
XNamespace soap = "http://www.w3.org/2001/12/soap-envelope"; 
XNamespace m = "http://www.example.org/stock"; 
var responseXml = xdoc.Element(soap + "Envelope").Element(soap + "Body") 
         .Element(m + "GetStockPriceResponse"); 

var serializer = new XmlSerializer(typeof(GetStockPriceResponse)); 
var responseObj = 
     (GetStockPriceResponse)serializer.Deserialize(responseXml.CreateReader()); 


[XmlRoot("GetStockPriceResponse", Namespace="http://www.example.org/stock")] 
public class GetStockPriceResponse 
{ 
    public decimal Price { get; set; } 
} 

당신은 당신의 OCTE 클래스와 동일한 기능을 수행 할 수 있습니다 : 클래스는 객체를 직렬화.

[XmlRoot("INCLUIRCONHECIMENTOFRETESAIDA",Namespace="http://192.168.1.180:8085/")] 
public class OCTE 
{ 
    // with property mapping to CONHECIMENTOFRETE, etc. 
} 
+0

미안하지만 그 대답은 무엇인지 모르겠군요. webservice는 응답을 반환하지 않습니다. 나는 OP에서 내 XML의 중요한 부분을 넣었다. 감사합니다. – gog

+0

@ggui 응답은 INCLUIRCONHECIMENTOFRETESAIDA를 반영하는 모든 클래스에 해당합니다. 나는 이것을 OCTE라고 가정하고, serializer가 그것을 이해하는 데 필요한 어떤 종류의 속성을 보여주는 코드를 추가했습니다. –

+0

예, OCTE 클래스입니다. octe.xml 파일의 정보를 얻은 다음 OCTE 객체에 배치하여 매개 변수로 webservice에 보냅니다. 당신의 해결책을 시험해보십시오. 감사! – gog

1

나는이 생각하는 당신은 내가 당신을 위해 네임 스페이스 및 요소 이름을 기입 할 수있는 충분한 세부 사항이없는

removing/extracting soap header and body from soap message

+0

ok하지만 모든 노드 (그 거대한 XML, 그리고 OCTE 객체는 XML의 모든 속성을 가짐)를 얻을 필요가있다. 내가 전에 내가 말한대로 예제를 사용하면 어떤 치료 (단 하나를 제외하고 SOAP 물건을 제거하지 않고 OCTE 객체로 파일을 비 직렬화 할 수 있습니다) – gog

관련 문제