This question already has an answer here:
XmlTypeAttribute works only on attributes in class
(1个答案)
5年前关闭。
我正在尝试修改WCF服务,并且在理解响应中的名称空间和xmlns标签时遇到问题。
此dataType在Iservice文件中,并具有名称空间的标记:
在响应中,我得到以下结果。我不明白为什么我在每个属性中都得到xlmns标记,我只想在DiscrepancyType标记中包含该域。我怎样才能解决这个问题?
(1个答案)
5年前关闭。
我正在尝试修改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>
最佳答案
请看看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();
}
10-07 16:53