在WCF XML输出中控制命名空间前缀 [英] Controlling Namespace Prefixes in WCF XML Output
问题描述
我的WCF服务的当前输出如下(下面仅显示了一部分):
The current output of my WCF service is as follows (only a portion is shown below):
<s:Body>
<executeSelectSP2Response xmlns="http://tempuri.org/">
<executeSelectSP2Result xmlns:a="http://schemas.datacontract.org/2004/07/WCF_Services.DataContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Rows>
<a:RowDetail>
<a:Fields>
<a:FieldDetail>
<a:name>STATE_CD</a:name>
<a:value>1</a:value>
</a:FieldDetail>
<a:FieldDetail>
<a:name>STATE_CD_TXT</a:name>
<a:value>Alabama</a:value>
</a:FieldDetail>
<a:FieldDetail>
<a:name>STATE_CD_SHORT_TXT</a:name>
<a:value>AL</a:value>
</a:FieldDetail>
</a:Fields>
</a:RowDetail>
在此示例中,每个美国州都重复使用"RowDetail"元素.
In the example, the "RowDetail" element is repeated for each US state.
我有两个问题:
-
如何删除标签中的"a:"前缀.我假设我需要更改xmlns设置,但是我不确定如何执行此操作而不会引发错误.我查看了其他线程,但无法正常工作.
How can I remove the "a:" prefix in the tags. I am assuming i need to change the xmlns setting, but I am not sure how to do this without throwing an error. I viewed other threads, but could not get it to work.
是否可以从输出中删除无关的元素,即行"和字段"?我知道为什么会出现这些-这是由于我如何设置我的课程(在下面发布),但是看IMO太麻烦了.
Is it possible to remove the extraneous elements from the output, namely "Rows" and "Fields"? I understand why they are there - it is due to how I set up my classes (posted below), but it is messy to look at IMO.
课程:
[DataContract]
public class Results2Detail
{
[DataMember]
public RowDetail[] Rows;
}
[DataContract]
public class RowDetail
{
[DataMember]
public FieldDetail[] Fields;
}
[DataContract]
public class FieldDetail
{
[DataMember]
public String name;
[DataMember]
public String value;
}
推荐答案
此处的问题如下:
-
您有一些与
executeSelectSP2Response
相对应的外部类(问题中未显示),该类在被序列化时放置在默认名称空间"http://tempuri.org/"
中.您可能不希望这样做,因为它是测试ASP.Net Web服务的默认名称空间,并且您应该将其替换为公司特定的名称空间.
You have some outer class corresponding to
executeSelectSP2Response
(not shown in your question) that is being placed in a default namespace"http://tempuri.org/"
while being serialized. You probably don't want this since it is the test default namespace for ASP.Net Web Services and you are expected to replace it with a company-specific namespace.
有关更换它的说明,请参见这里.
For instructions in replacing it, see here or here.
NameSpace
属性,因此 中.这不同于其父元素的默认名称空间,因此必须指定替代名称空间. a:
前缀引用xmlns:a="http://schemas.datacontract.org/2004/07/WCF_Services.DataContract"
属性,并指定每个这样标记的元素都属于该命名空间.
The DataContract
attributes for the classes shown have no NameSpace
property, so by default your classes are all to be serialized into the namespace "http://schemas.datacontract.org/2004/07/Clr.Namespace"
. This differs from the default namespace of their parent element so an override namespace must be specified. The a:
prefixes refer to the xmlns:a="http://schemas.datacontract.org/2004/07/WCF_Services.DataContract"
attribute and specifies that each element so tagged belongs in that namespace.
如果要指定Results2Detail
等. al.您可以这样做:
If you want to specify that Results2Detail
et. al. do not belong in a specific namespace (i.e. inherit their namespace from their parent) you can do:
[DataContract(Namespace="")]
public class Results2Detail
{
[DataMember]
public RowDetail[] Rows;
}
[DataContract(Namespace = "")]
public class RowDetail
{
[DataMember]
public FieldDetail[] Fields;
}
[DataContract(Namespace = "")]
public class FieldDetail
{
[DataMember]
public String name;
[DataMember]
public String value;
}
如果需要特定的名称空间,则可以执行[DataContract(Namespace = Namespaces.CompanyNameSpace)]
,其中Namespaces
是一些静态类,例如:
If you want a specific namespace, you can do [DataContract(Namespace = Namespaces.CompanyNameSpace)]
where Namespaces
is some static class like:
public static class Namespaces
{
const string CompanyNameSpace = "http://company.namespace.org"; // or whatever.
}
您的问题2尚不清楚.您是说要让数组显示为单个元素级别,而不是两个嵌套元素级别,即:
Your question #2 is unclear. Are you saying you want your arrays to appear as a single level of elements rather than two nested levels of elements, i.e.:
<executeSelectSP2Result>
<RowDetail>
<FieldDetail>
</FieldDetail>
</RowDetail>
<RowDetail>
<FieldDetail>
</FieldDetail>
</RowDetail>
</executeSelectSP2Result>
如果是,则为否,使用DataContractSerializer
不能立即实现此级别的控制.您必须实现IXmlSerializable
并手动执行操作,或者切换到XmlSerializer
并使用
If so, then no, this level of control is not immediately possible with DataContractSerializer
. You must either implement IXmlSerializable
and do it manually, or switch to XmlSerializer
and decorate your arrays with the XmlElement
attribute.
这篇关于在WCF XML输出中控制命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!