ServiceContractGenerator type problem
We try to generate web service proxy class using ServiceContractGenerator.
We get problem at using web services that returns DataTable.Generated code for DataTable below
public partial class MethodResultOfDataTableymujubN2Result
{
private System.Xml.XmlElement[] anyField;
private System.Xml.XmlElement any1Field;
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute(Namespace="http://www.w3.org/2001/XMLSchema", Order=0)]
public System.Xml.XmlElement[] Any
{
get
{
return this.anyField;
}
set
{
this.anyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute(Namespace="urn:schemas-microsoft-com:xml-diffgram-v1", Order=1)]
public System.Xml.XmlElement Any1
{
get
{
return this.any1Field;
}
set
{
this.any1Field = value;
}
}
}
References.cs class can handle DataTable, and return DataTable object
[System.Runtime.Serialization.DataMemberAttribute()]
public System.Data.DataTable Result { ...
How can we handle DataTable f开发者_StackOverflow社区or generating web service proxy class?
below method solve the problem
WsdlImporter wsdlImporter = CreateWsdlImporter(address);
AddStateForDataContractSerializerImport(wsdlImporter);
...
private void AddStateForDataContractSerializerImport(WsdlImporter importer)
{
XsdDataContractImporter xsdDataContractImporter = new XsdDataContractImporter();
xsdDataContractImporter.Options = new ImportOptions();
xsdDataContractImporter.Options.ImportXmlType = true;
xsdDataContractImporter.Options.GenerateSerializable = true;
xsdDataContractImporter.Options.ReferencedTypes.Add(typeof(DataTable));
importer.State.Add(typeof(XsdDataContractImporter), xsdDataContractImporter);
}
I have faced the same problem and after investigating I did the following which solved the issue:
usually you will find two properties in the class causing the error:
private System.Xml.Linq.XElement[] anyField; private System.Xml.Linq.XElement any1Field;
What I did was the following:
1- changed the first property from an array to a single value variable as follows
private System.Xml.Linq.XElement anyField;
2- change the getter and setter methods of this property , to match your changes
[System.Xml.Serialization.XmlAnyElementAttribute(Namespace="http://www.w3.org/2001/XMLSchema", Order=0)]
public System.Xml.Linq.XElement Any {
get {
return this.anyField;
}
set {
this.anyField = value;
this.RaisePropertyChanged("Any");
}
}
3- remove or comment out the second property
// private System.Xml.Linq.XElement any1Field;
4- remove or comment out the second property's getter and setter methods
/*
[System.Xml.Serialization.XmlAnyElementAttribute(Namespace="urn:schemas-microsoft-com:xml-diffgram-v1", Order=1)]
public System.Xml.Linq.XElement Any1 {
get {
return this.any1Field;
}
set {
this.any1Field = value;
this.RaisePropertyChanged("Any1");
}
}
*/
At this point you can now access the resulting XML as follows by calling the "Any" property which will return an xml which you can manipulate :
e.g., in my case it was the following class causing the problems
public partial class GetUserBalancesClassAccounts
In my method I was able to access the xml as follows
GetUserBalancesClassAccounts accts = balances.Accounts;
XElement doc = accts.Any;
foreach( XElement docElement in doc.Elements()){
foreach (XElement account in docElement.Elements("Account"))
{
... do something ...
}
}
精彩评论