C# 如何使用WCF在SOAP请求中包含空xml元素?
我必须使用C# 如何使用WCF在SOAP请求中包含空xml元素?,c#,xml,web-services,wcf,C#,Xml,Web Services,Wcf,我必须使用Javaweb服务。服务方法的签名是 public bool UpsertEmployee(Employee employe); 问题是,当生成SOAP时,对于具有null值的属性,请求中不包括相应的XML元素。结果是: ... <Employee> <id>1</id> <firstName>Jhonny</firstName> </Employee> 生成的员工类别代码为 [System.CodeD
Java
web服务。服务方法的签名是
public bool UpsertEmployee(Employee employe);
问题是,当生成SOAP
时,对于具有null值的属性,请求中不包括相应的XML
元素。结果是:
...
<Employee>
<id>1</id>
<firstName>Jhonny</firstName>
</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));
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Xml”,“4.0.30319.36366”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace=“urn:mve.go.all.mdg.vendor”)]
公共部分类Employee:对象,System.ComponentModel.INotifyPropertyChanged
{
专用字符串字段;
私有字符串firstNameField;
私有字符串lastNameField;
///
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,Order=0)]
公共字符串id
{
得到
{
返回此.idField;
}
设置
{
this.idField=值;
本.RaisePropertyChanged(“id”);
}
}
///
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,Order=1)]
公共字符串名
{
得到
{
返回此.firstNameField;
}
设置
{
this.firstNameField=值;
本.RaiseProperty变更(“名字”);
}
}
///
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,Order=3)]
公共字符串姓氏
{
得到
{
返回此.lastname字段;
}
设置
{
this.lastNameField=值;
此.RaisePropertyChanged(“姓氏”);
}
}
公共事件System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
受保护的void RaisePropertyChanged(字符串propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged=this.propertyChanged;
如果((propertyChanged!=null))
{
propertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
我无法修改文件代码,因为将来可能需要更新服务引用。您的类标记为,因此您似乎正在使用该序列化程序
您的问题是lastName
属性是null
。如中所述:
将对象序列化为XML文档时:如果XmlSerializer
类遇到对应于XML元素的对象的空引用,它会生成一个指定xsi:nil=“true”
的元素,或者完全忽略该元素,这取决于nillable=“true”
设置是否适用
因此,默认情况下,当lastName
为null时,不会发出任何元素。如果你要设定,你会得到
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>1</id>
<firstName>Jhonny</firstName>
<lastName xsi:nil="true" />
</Employee>
然后将生成以下XML:
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>1</id>
<firstName>Jhonny</firstName>
<lastName />
</Employee>
这可能是员工定义的结果
Employee
可能是您的自定义类,因此应将其定义为DataContract
,并将其属性定义为DataMember
s。在DataMember
中,有一个名为EmitCustomValue
的设置(默认为true)。它控制SOAP中是否包含空值。有关更多信息,请参阅。查看Employee
类定义,并验证EmitDefaultValue
未设置为false
。服务是用Java
编写的,我已检查了文件Reference.cs
(通过按服务类名上的f12
),并且没有DataContract
属性,相反,所有属性都具有xmlementattribute
属性,我无法修改文件代码,因为将来可能需要更新服务引用。需要查看Employee
类才能确定,但设置lastName=“
应该可以工作。两个c#xml序列化程序都将空字符串序列化为空元素,并在默认情况下完全跳过空字符串。我为Employee
类添加了自动生成的代码。该类是在t添加对Java
web服务的引用后生成的。
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>1</id>
<firstName>Jhonny</firstName>
<lastName xsi:nil="true" />
</Employee>
var employee = new Employee
{
id = "1",
firstName = "Jhonny",
lastName = "",
};
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>1</id>
<firstName>Jhonny</firstName>
<lastName />
</Employee>
public partial class Employee
{
public Employee()
{
this.firstName = this.lastName = this.id = "";
}
}