Controlling Namespace Prefixes in WCF XML Output

孤街醉人 提交于 2019-12-05 18:37:24

The issues here are as follows:

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

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

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

    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.
    }
    
  3. 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>
    

    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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!