理解WCF响应中的xmlns标记

本文关键字:xmlns 标记 WCF 响应 理解 | 更新日期: 2023-09-27 18:27:36

我试图修改WCF服务,但在理解响应中的名称空间和xmlns标记时遇到问题。

此dataType位于Iservice文件中,并具有命名空间的标记:

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.2152")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.zz.com/x/Domain")]
public partial class DiscrepancyType
{
    private string barcodeField;
    private string classificationField;
    private bool commentRequiredField;
    private string descriptionField;
    private int discrepancyTypeIdField;
    private System.Nullable<int> usageRankField;
    private string useCaseIdField;
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public string Barcode
    {
        get
        {
            return this.barcodeField;
        }
        set
        {
            this.barcodeField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public string Classification
    {
        get
        {
            return this.classificationField;
        }
        set
        {
            this.classificationField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=2)]
    public bool CommentRequired
    {
        get
        {
            return this.commentRequiredField;
        }
        set
        {
            this.commentRequiredField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=3)]
    public string Description
    {
        get
        {
            return this.descriptionField;
        }
        set
        {
            this.descriptionField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=4)]
    public int DiscrepancyTypeId
    {
        get
        {
            return this.discrepancyTypeIdField;
        }
        set
        {
            this.discrepancyTypeIdField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=5)]
    public System.Nullable<int> UsageRank
    {
        get
        {
            return this.usageRankField;
        }
        set
        {
            this.usageRankField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=6)]
    public string UseCaseId
    {
        get
        {
            return this.useCaseIdField;
        }
        set
        {
            this.useCaseIdField = value;
        }
    }
}

在响应中,我得到以下结果。我不明白为什么我在每个属性中都有xlmns标记,我只想在DifferencyType标记中有那个域。我该怎么解决这个问题?

<DiscrepancyType>
               <Barcode xmlns="http://www.zz.com/x/Domain"></Barcode>
               <Classification xmlns="http://www.zz.com/x/Domain"></Classification>
               <CommentRequired xmlns="http://www.zz.com/x/Domain"></CommentRequired>
               <Description xmlns="http://www.zz.com/x/Domain"></Description>
               <DiscrepancyTypeId xmlns="http://www.zz.com/x/Domain"></DiscrepancyTypeId>
               <UsageRank xmlns="http://www.zz.com/x/Domain"></UsageRank>
               <UseCaseId xmlns="http://www.zz.com/x/Domain"></UseCaseId>
</DiscrepancyType>

理解WCF响应中的xmlns标记

请查看在.NET 中序列化对象时省略所有xsi和xsd命名空间

最重要的部分是在序列化时清除命名空间。示例序列化方法可能看起来像:

public string Serialize()
{
  var writer = new StringWriter();
  var ns = new XmlSerializerNamespaces();
  ns.Add("", "");
  var serializer = new XmlSerializer(typeof(DiscrepancyType));
  serializer.Serialize(writer, this, ns);
  return writer.ToString();
}

默认情况下的行为。名称空间有问题吗?您可以通过将DataContract属性的namespace参数设置为空字符串来移除XML命名空间