理解WCF响应中的xmlns标记

时间:2014-10-23 08:45:13

标签: c# xml wcf xml-namespaces

我正在尝试修改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标记,我希望只在DiscrepancyType标记中包含该域。我该如何解决这个问题?

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

2 个答案:

答案 0 :(得分:0)

请查看Omitting all xsi and xsd namespaces when serializing an object in .NET

最重要的部分是在序列化时清除命名空间。示例序列化方法可能如下所示:

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

答案 1 :(得分:0)

默认情况下它的行为。你有命名空间的问题吗?您可以通过将DataContract属性的Namespace参数设置为空字符串

来删除XML命名空间