命名空间不附加到WCF中的bool值,除非与class's命名空间不同

本文关键字:命名空间 class WCF 中的 bool | 更新日期: 2023-09-27 18:05:18

我有一个在WCF中用于发送SOAP响应的数据类型。它看起来像这样:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <start-call-recording-response xmlns="http://foobar">
         <response>true</response>
      </start-call-recording-response>
   </s:Body>
</s:Envelope>

问题是名称空间(http://foobar)没有出现在元素中。也就是说,除非我将xmlelementattribute的名称空间更改为与父类的XmlRootAttribute的名称空间不同的名称空间。下面是start-call-recording-response的类:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18033")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "StartcallrecordingresponseType")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://foobar", ElementName = "start-call-recording-responseType")]
public partial class StartcallrecordingresponseType
{
    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://foobar")]
    private bool responseField;
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://foobar", ElementName = "response", IsNullable = false)]
    public bool Response
    {
        get
        {
            return this.responseField;
        }
        set
        {
            this.responseField = value;
        }
    }
}

如果我将Response上面的XmlElementAttribute的名称空间更改为包含类的名称空间以外的其他内容,则它将出现在SOAP信封的名称空间中。如果它们是一样的,它就不会出现。尝试了许多XmlTypeAttributes、XmlRootAttributes和XmlElementAttributes的变体

命名空间不附加到WCF中的bool值,除非与class's命名空间不同

正确。参见命名空间规范

默认名称空间声明的作用域从它出现的起始标记开始延伸到相应的结束标记结束,不包括任何内部默认名称空间声明的作用域。在空标记的情况下,作用域是标记本身。

所以response元素从它的父元素start-call-recording-response继承了foobar命名空间