當我得到一個XML,我需要將其反序列化到一個spec對象並通過參數傳遞給一個web服務方法。如何忽略將反序列化xml到對象的soap東西?
代碼:
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);
但是當我嘗試反序列化,我得到一個錯誤說
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" > was not expected.
我需要忽略信封標籤和其他SOAP的東西。 我該怎麼做?
編輯:
的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時,我將所有值都設爲null。
使用'XDocument'並執行'document.Descendant(「OCTE」)'? (或任何根名應該是) –
後裔,不知道如果我可以找到這個成員在XmlDocument類。 – gog
對,我把它與'XDocument'混淆了。你可以用那個班嗎?無論哪種方式,我相信有一種方法可以將XML樹導航到您的身體價值。 –