如何使用WCF在SOAP请求中包含空的xml元素?

时间:2017-05-18 07:27:49

标签: c# xml web-services wcf

我必须使用Java网络服务。服务方法的签名是

public bool UpsertEmployee(Employee employe);

问题是,在生成SOAP时,对于具有null值的属性,请求中不包含相应的XML元素。结果是:

...
<Employee>
  <id>1</id>
  <firstName>Jhonny</firstName>
</Employee>

我想成为:

...
<Employee>
  <id>1</id>
  <firstName>Jhonny</firstName>
  <lastName/>
</Employee>

有没有办法实现这个目标?

我可以在mehod调用之前设置属性吗?

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));
        }
    }
}

我无法修改文件代码,因为将来可能需要更新服务引用。

1 个答案:

答案 0 :(得分:3)

您的课程标有XmlSerializer attributes,因此您似乎正在使用该序列化程序。

您的问题是lastName属性为null。如 Xsi:nil Attribute Binding Support 中所述:

  

将对象序列化为XML文档时:如果XmlSerializer类遇到对应于XML元素的对象的空引用,则它会生成一个指定xsi:nil="true"的元素或完全保留该元素,取决于nillable="true"设置是否适用。

因此,默认情况下,当lastName为null时,不会发出任何元素。如果你要设置[XmlElementAttribute(IsNullable = true)],你会得到

<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>

你想要什么(无论如何你都无法更改自动生成的代码)。

相反,您需要将lastName初始化为空字符串:

var employee = new Employee
{
    id = "1",
    firstName = "Jhonny",
    lastName = "",
};

然后将生成以下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>

或者,由于自动生成的代码没有默认构造函数,您可以在单独的partial class代码文件中自行添加一个。由于它独立于自动生成的代码文件,因此在重新生成生成的代码时不会被覆盖:

public partial class Employee
{
    public Employee()
    {
        this.firstName = this.lastName = this.id = "";
    }
}