【发布时间】:2017-05-18 07:27:49
【问题描述】:
我必须使用Java 网络服务。服务方法的签名是
public bool UpsertEmployee(Employee employe);
问题是在生成SOAP 时,对于具有空值的属性,请求中不包含对应的XML 元素。结果是:
...
<Employee>
<id>1</id>
<firstName>Jhonny</firstName>
</Employee>
我想成为:
...
<Employee>
<id>1</id>
<firstName>Jhonny</firstName>
<lastName/>
</Employee>
有没有办法做到这一点?
我可以在方法调用之前设置属性吗?
var client= new EmployeeServiceClient();;
// Can I do something here to accoplish my goal?
client.UpsertEmployee(new Employee{
id = "1",
firstname = "Jhonny"
});
生成的Employee类代码是
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.36366")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:mve.go.all.mdg.vendor")]
public partial class Employee : object, System.ComponentModel.INotifyPropertyChanged
{
private string idField;
private string firstNameField;
private string lastNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
this.RaisePropertyChanged("id");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
public string firstName
{
get
{
return this.firstNameField;
}
set
{
this.firstNameField = value;
this.RaisePropertyChanged("firstName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 3)]
public string lastName
{
get
{
return this.lastNameField;
}
set
{
this.lastNameField = value;
this.RaisePropertyChanged("lastName");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
我无法修改文件代码,因为将来可能需要更新服务参考。
【问题讨论】:
-
可能是
Employee定义的结果。Employee可能是您的自定义类,因此应将其定义为DataContract,并将其属性定义为DataMembers。在DataMember中,有一个名为EmitCustomValue的设置(默认为true)。它控制 SOAP 中是否包含空值。有关更多信息,请查看msdn.microsoft.com/en-us/library/…。查看Employee类定义并验证EmitDefaultValue是否未 设置为false。 -
服务写在
Java,我检查了文件Reference.cs(通过在服务类名称上按f12)并且没有DataContract属性,而是所有属性都有XmlElementAttribute属性,我无法修改文件代码,因为将来可能需要更新服务引用。 -
需要查看
Employee类以确保,但设置lastName = ""应该可以工作。两个 c# xml 序列化器都将空字符串序列化为空元素,默认情况下完全跳过空字符串。 -
我已经更新了我的问题。我为
Employee类添加了自动生成的代码。在添加对Javaweb 服务的引用后生成的类。
标签: c# xml web-services wcf