Click here to Skip to main content
14,981,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

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, exact 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;

C#
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:

XML
<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.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
Posted
Comments
[no name] 23-Jan-11 9:35am
   
What do you mean "unable to deserialize"? Do you get exceptions? Do you get partial data? What does the does the object you trying to use look like? What approaches have you tried? You have left may questions unanswered that would help us to help you.
Sergey Alexandrovich Kryukov 23-Jan-11 10:49am
   
This XML is well-formed and it conforms to some schema that should be written in XSD. Get you get XSD (XSDs) for this data? Do you have WSDL-generated class(es) (I think you do)? Can you post this class(es)?
Anyway, I don't see a problem. Maybe you need to post an example of non-well-formed XML you refer to.
cohay_indonesia 6-Feb-11 23:41pm
   
You can go web service C#....!!! ^_^
Or AJAX !!!
cohay_indonesia 6-Feb-11 23:44pm
   
[WebMethod]
public string save_admin(string nama, string sandi)
{
string sql = "insert into admin values ('" + nama + "', '" + sandi + "')";
MySqlDataReader dread = nyambung.EksekusiReader(sql);
dread.Read();
string xcohayx = null;
return xcohayx;
}

Just example XD

I don't see the problem from you samples. As I already said, your sample XML is well-formed (did you think it is not?). Probably, you need to post a sample of XML received from the service which is not well-formed. You mentioned this case in your answer.

Another possibility I can see is that you misused terminology. When you say "not well formatted xml file", could you mean that in fact the file is well-formed but does not conform to the expected schema? Please check up. If this is the case, the possible problem is WSDL mismath: for example, you use older version then the service or something like that. This is easy to check up.

Anyway you can see a lot of relevant discussions if you Google:

http://en.lmgtfy.com/?q=%22WSDL-generated%22+deserialze+%22C%23%22[^].

—SA
   
v6
Comments
Espen Harlinn 6-Feb-11 14:55pm
   
A 5, once again :)
   
Thank you, Espen.
I even improved the answer...
--SA
Nish Nishant 6-Feb-11 21:28pm
   
Voted 5, good answer, and points the OP in the right directions.
   
Thank you, Nishant, hope it helps.
--SA
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.

C#
[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!
   
Probably I'm the problem, need to work less.

First I tried to deserialize it in a standard way:

XmlSerializer serializer = new XmlSerializer(typeof(CoverDecisionType));

CoverDecisionType a = CoverDecisionType)serializer.Deserialize(reader); 


And I was getting InvalidOperationException ({"There is an error in XML document (1, 2)."}
{"<CoverDecision xmlns='http://atradius.com/connect/_2007_08/'> was not expected."})

Why I tried this? This we service has a couple of methods. Many of these methods replay with CoverDecisionType, but not this one, this one replays with an byte array that is a zip file, containing an xml file with a multiple root elements. Each of these elements should be CoverDecisionType object. CoverDecisionType object was defined by WSE3 basing the proxy on WSDL.
But, right now, analyzing the trace info of the methods that returns CoverDecisionType, I see that the XML contained into the SOAP message body differs with the code that I listed.
As I'm not an XML expert, I would like to know if it is possible to deserialize this XML into the same CoverDecisionType object. If not, what should be the best way to proceed?

The CoverDecisionType definition is (partial):

MSIL
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://atradius.com/connect/_2007_08/")]
public partial class CoverDecisionType
{
    private string externalCoverIdField;
    private OrganisationType buyerField;
    private BasicOrganisationType customerField;
    private long policyIdField;
    private BasicRequestSummaryType itemField;
    private ItemChoiceType itemElementNameField;
    private DecisionType decisionField;
    private long coverIdField;
    private CoverType typeOfCoverField;
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string ExternalCoverId
    {
        get
        {
            return this.externalCoverIdField;
        }
        set
        {
            this.externalCoverIdField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public OrganisationType Buyer
    {
        get
        {
            return this.buyerField;
        }
        set
        {
            this.buyerField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public BasicOrganisationType Customer
    {
        get
        {
            return this.customerField;
        }
        set
        {
            this.customerField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public long PolicyId
    {
        get
        {
            return this.policyIdField;
        }
        set
        {
            this.policyIdField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("CreditCheckApplication", typeof(BasicApplicationSummaryType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("CreditLimitApplication", typeof(AmountLimitApplicationSummaryType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("GenericApplication", typeof(BasicRequestSummaryType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("NoRiskOpinionApplication", typeof(AmountLimitApplicationSummaryType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("SpecialProductApplication", typeof(AmountLimitApplicationSummaryType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
    public BasicRequestSummaryType Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemChoiceType ItemElementName
    {
        get
        {
            return this.itemElementNameField;
        }
        set
        {
            this.itemElementNameField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public DecisionType Decision
    {
        get
        {
            return this.decisionField;
        }
        set
        {
            this.decisionField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public long CoverId
    {
        get
        {
            return this.coverIdField;
        }
        set
        {
            this.coverIdField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public CoverType TypeOfCover
    {
        get
        {
            return this.typeOfCoverField;
        }
        set
        {
            this.typeOfCoverField = value;
        }
    }
}


and the soap message that is correctly deserialized is:

XML
<soapenv:body xmlns:soapenv="#unknown">
  <applyforcreditlimitresponse xmlns="http://atradius.com/connect/_2007_08/">
	<coverdecision typeofcover="CreditLimit" coverid="123123">
	  <buyer>
		<ns1:identifier registeredoffice="SYMPH" xmlns:ns1="http://atradius.com/organisation/_2007_08/type/">
		  <ns1:id>123123</ns1:id>
		  <ns1:countrytypeidentifier>NLD</ns1:countrytypeidentifier>
		</ns1:identifier>
		<ns2:identifier registeredoffice="COC" xmlns:ns2="http://atradius.com/organisation/_2007_08/type/">
		  <ns2:id>12312312312</ns2:id>
		  <ns2:countrytypeidentifier>NLD</ns2:countrytypeidentifier>
		</ns2:identifier>
		<ns3:name xmlns:ns3="http://atradius.com/organisation/_2007_08/type/">
		  <ns3:name> BEDRIJVENCONTACTDAGAMSTERDAM</ns3:name>
		  <ns3:type>REG</ns3:type>
		</ns3:name>
		<ns4:legalform xmlns:ns4="http://atradius.com/organisation/_2007_08/type/">STIC</ns4:legalform>
		<ns5:address xmlns:ns5="http://atradius.com/organisation/_2007_08/type/">
		  <ns5:streetdescription>DE 1081-A</ns5:streetdescription>
		  <ns5:city>AMSTERDAM</ns5:city>
		  <ns5:postcode>1081 HV</ns5:postcode>
		  <ns5:countryisocode>NLD</ns5:countryisocode>
		</ns5:address>
	  </buyer>
	  <customer>
		<ns6:identifier registeredoffice="SYMPH" xmlns:ns6="http://atradius.com/organisation/_2007_08/type/">
		  <ns6:id>123123123</ns6:id>
		  <ns6:countrytypeidentifier>NLD</ns6:countrytypeidentifier>
		</ns6:identifier>
		<ns7:identifier registeredoffice="COC" xmlns:ns7="http://atradius.com/organisation/_2007_08/type/">
		  <ns7:id>123123123</ns7:id>
		  <ns7:countrytypeidentifier>NLD</ns7:countrytypeidentifier>
		</ns7:identifier>
		<ns8:name xmlns:ns8="http://atradius.com/organisation/_2007_08/type/">
		  <ns8:name>NL B.V.</ns8:name>
		  <ns8:type>REG</ns8:type>
		</ns8:name>
	  </customer>
	  <policyid>123123</policyid>
	  <creditlimitapplication>
		<customerreference />
		<entrydate>2011-01-20</entrydate>
		<supersede>false</supersede>
		<requestedamount>10000</requestedamount>
		<currencycode>EUR</currencycode>
	  </creditlimitapplication>
	  <decision>
		<applicationresult>ReferedDecision</applicationresult>
		<decisiondate>2011-01-20</decisiondate>
		<effectfrom>2011-01-20</effectfrom>
		<effectto>4712-12-31</effectto>
		<referreddecision xsi:type="ns9:String35" xmlns:ns9="http://atradius.com/organisation/_2007_08/type/" xmlns:xsi="#unknown">REFERRED</referreddecision>
	  </decision>
	</coverdecision>
  </applyforcreditlimitresponse>
</soapenv:body>


Any suggestion?

Thanks
   
v2
Comments
Mario Majčica 23-Jan-11 15:40pm
   
Please ignore xmlns:soapenv="#unknown" attributes, they are added automatically by codeproject.
[no name] 23-Jan-11 16:33pm
   
Do not add as an answer. Update your original question with additional information.
cohay_indonesia 6-Feb-11 23:45pm
   
lol

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900