Removing Deserialization of a Specific XML String

I have a problem and I cannot get out of it. My knowledge of WebServices is small, and I have a small problem that I need to solve. I am developing a client for a web service, and I do not have authority over the server side web service (and I think it is developed in Java). I used WSE3 for my client, and it seems to work very well, except for a few methods that I cannot solve. Based on my definition of WSDL, I generated my proxy classes with the correct data types and methods to call. Many of these methods return already deserialized SOAP messages that are discarded to the appropriate object type. Unfortunately, some of them send back a byte array of a ZIP file containing an unformatted XML file inside it. I managed to get the stream, unzip the file and read the xml, but I can not deserialize the XML file and then apply it to the appropriate type. This is an example of my code and an example xml that I need to deserialize and apply to the appropriate type. Do you have any suggestions?

MyClient client = new MyClient(ServiceSettings); ConnectResponseRetrieveMyType data; try { // call web service method data = client.syncData(service, startDate, endDate); // unzip the byte array using (ZipFile zip = ZipFile.Read(data.Data)) { if (zip.ContainsEntry("data.xml")) { List<string> strings = new List<string>(); // read the xml file with multiple root elements XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; using (XmlReader reader = XmlReader.Create(zip["data.xml"].OpenReader(), settings)) { while (reader.Read()) { strings.Add(reader.ReadOuterXml()); } } } else return "OGZIP01"; } } 

At the end, I have a List <> string containing this data:

 <c:CoverDecision TypeOfCover="CreditLimit" CoverId="123123123" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:c="http://atradius.com/connect/_2007_08/" xmlns:o="http://atradius.com/organisation/_2007_08/type/"> <Buyer> <o:Identifier registeredOffice="SYMPH"> <o:id>123123123</o:id> <o:countryTypeIdentifier>AUT</o:countryTypeIdentifier> </o:Identifier> <o:Identifier registeredOffice="COC"> <o:id>123123123F</o:id> <o:countryTypeIdentifier>AUT</o:countryTypeIdentifier> </o:Identifier> <o:Name> <o:name>SOME GES.MBH</o:name> <o:type>REG</o:type> </o:Name> <o:LegalForm>GMBH</o:LegalForm> <o:Address> <o:StreetDescription xsi:type="xsd:string">STRAßE 49</o:StreetDescription> <o:City>FÜRSTENFELD</o:City> <o:PostCode>23123</o:PostCode> <o:CountryISOCode>AUT</o:CountryISOCode> </o:Address> </Buyer> <Customer> <o:Identifier registeredOffice="SYMPH"> <o:id>123123</o:id> <o:countryTypeIdentifier>NLD</o:countryTypeIdentifier> </o:Identifier> <o:Identifier registeredOffice="COC"> <o:id>123123</o:id> <o:countryTypeIdentifier>NLD</o:countryTypeIdentifier> </o:Identifier> <o:Name> <o:name>SOME BV</o:name> <o:type>REG</o:type> </o:Name> </Customer> <PolicyId>123123</PolicyId> <GenericApplication> <CustomerReference>123123</CustomerReference> <EntryDate>2010-02-04</EntryDate> <Supersede>false</Supersede> </GenericApplication> <Decision> <ApplicationResult>CreditLimitDecision</ApplicationResult> <DecisionDate>2010-02-05</DecisionDate> <EffectFrom>2010-02-05</EffectFrom> <EffectTo>2010-07-19</EffectTo> <CreditLimitDecision> <CreditLimitResultCode>APPR</CreditLimitResultCode> <DecisionCode>DC16</DecisionCode> <FirstAmount> <Amount>150000.00</Amount> <Conditions> <TypeOfConditions>ADMIN</TypeOfConditions> <ConditionCode>T310</ConditionCode> <ConditionText>Some condition description text.</ConditionText> </Conditions> </FirstAmount> <SecondAmount> <Amount>0</Amount> </SecondAmount> </CreditLimitDecision> </Decision> </c:CoverDecision> 

And I was not able to deserialize it and apply it to the corresponding type of object. I tried many approaches, but I was unsuccessful. Do you have any suggestions?

thanks

+4
source share
3 answers

Got an answer. Tanks Yang! Apparently, XmlSerializer did not know how to handle namespaces and the root element.

This attribute helps solve the problem.

 [System.Xml.Serialization.XmlRootAttribute("CoverDecision", Namespace = "http://atradius.com/connect/_2007_08/", IsNullable = false)] public partial class CoverDecisionType 

So thanks to everyone who helped me brainstorm!

+1
source

Yes, xml is not formatted. Each identifier element must be inside an identifier.

In addition, it is quite simple with other types (for an identifier, create a List<Identifier> in the Buyer and Consumer types.

The easiest way to read data is to use DataSet.ReadXml(xmlfile); And as soon as the data is downloaded, you will have a table for "CoverDecision", "Buyer", "Identifier", etc. This would also create relationships ( dataSet.Relations ), in your case 13 of them.

Thus, by moving tables using relationships, you can get all the data.

+1
source

In response to Vijay Sirigiri.

Thanks for the replay. I have already tried this solution and it works great:

 using (XmlReader reader = XmlReader.Create(zip["data.xml"].OpenReader(), settings)) { while (reader.Read()) { ds.ReadXml(reader); ... 

And it works just as you are sad. This may be a solution to the rebound, unless I find another.

Curiously, if I recall the method from WS that one CoverDecision should give me, I will return to the corresponding object, deserialized and cast. I was surprised if it is possible to repeat the same behavior that WSE3 uses to deserialize this object? This works fine with an example, I recall the WS method, and I return ConnectResponseType (SOAPResponse), which contains:

 CoverDecisionType response = SOAPResponse.CoverDecision; 

Do you have a clue on how to do this?

+1
source

Source: https://habr.com/ru/post/1336666/


All Articles