开发者

Deserializing a particular XML string

开发者 https://www.devze.com 2023-02-06 11:23 出处:网络
I have a problem and I can\'t get out of it. My knowledge about WebServices isn\'t great and I have a little problem that I need to solve.

I have a problem and I can't get out of it. My knowledge about WebServices isn't great and I have a little problem that I need to solve. I'm developing a client for the web service and I have no power over the server side web service (and I think it is developed in Java). I used WSE3 to base my client on and it seems working pretty fine, except for a couple of methods, which I can’t solve. Based on my WSDL definition I generated my proxy classes, whit proper data types and methods to call. Many of these methods return already deserialized SOAP messages, casted to the proper object type. Unfortunately some of them send back a byte array of a ZIP file, containing a not well formatted xml file inside. I managed to get the stream, unzip the file and read the xml, but I can’t deserialize properly the xml and then cast it on a respective type. This is an example of my code, and an example of the xml that I need to deserialize and cast to proper 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 an List<> of string containing this data:

<c:Co开发者_Go百科verDecision 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.M.B.H.</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 B.V.</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’m unable to deserialize it and cast it to the proper object type. I tried many approaches but I was unsuccessful. Perhaps you have any suggestion?

Thanks


Yes right, the xml is not formatted. Each 'identifier' element should have been inside 'identifiers'.

Other than that it is pretty simple with other types ( for identifier, create a List<Identifier> in Buyer and Consumer types.

The easiest way to read the data is to use DataSet.ReadXml(xmlfile); And once the data is loaded, you would have a table for "CoverDecision", "Buyer", "Identifier" and so on. It would also create Relations (dataSet.Relations), in your case 13 of them.

So navigating tables with the help of relations, you can get all the data.


In response to Vijay Sirigiri.

Thanks for the replay. I already tried that solution and it works fine:

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

And it works just as you sad. This can be a rebound solution if I don't find another one.

The curious thing is that if I recall the method from WS that should give me one CoverDecision, I get back the proper object, deserialized and casted. I was wondereng, is it possible to replicate the same behaviour that WSE3 uses to deserialize that object? At example this works fine, I recall the WS method and I get back ConnectResponseType (SOAPResponse) that contains:

CoverDecisionType response = SOAPResponse.CoverDecision;

Have a clue on how to do that?


Got the answer. Tanks Jan! Apparently the XmlSerializer didn't know how to treat the namespaces and the root element.

This attribute help to solve the problem.

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

So thanks to all who helped me brainstorming!

0

精彩评论

暂无评论...
验证码 换一张
取 消