避免嵌套成员 xml 序列化的命名空间

Avoid namespace for nested member xml serialisation

请问,在序列化 ResponseMessageEnvelope class 时,是否有任何方便的方法来避免嵌套成员 Request 的命名空间属性?结果 xml 将不会用于反序列化。

[XmlInclude(typeof(AuthenticateRequest))]    
[XmlRoot("REQUEST",Namespace="")]
public abstract class BaseRequest
{
 ...
}

[XmlRoot("REQUEST", Namespace = "")]
public class AuthenticateRequest : BaseRequest
{
 ...
}

[XmlRoot("EXTSYSTEM", Namespace="")]
public class ResponseMessageEnvelope
{        
    public ResponseMessageEnvelope(BaseRequest request, BaseReponse response)
    {
        Request = request;
        Response = response;
    }

    [XmlElement("REQUEST", Namespace = "")]        
    public Request.BaseRequest Request
    {
        get;
        set;
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");

        using (XmlWriter sWriter = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true ,Indent = true }))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ResponseMessageEnvelope));
            serializer.Serialize(sWriter, this, ns);                
            return sb.ToString();
        }
    }    
}

我尝试为基础和派生的 XmlRoot 属性设置空命名空间 class,也尝试为成员上的 XmlElement 设置空命名空间,但没有成功。谢谢指教。

<EXTSYSTEM>
  <REQUEST xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:type="AuthenticateRequest">
  ...        
  </REQUEST>
....
</EXTSYSTEM>   

这个我想要:

<EXTSYSTEM>
  <REQUEST>
  ...        
  </REQUEST>
....
</EXTSYSTEM> 

基础 class 属性 必须用已知类型修饰以避免在输出中出现这些声明。将 ResponseMessageEnvelope 中的请求属性更改为

[XmlElement("REQUEST")]
[XmlElement(Type = typeof(AuthenticateRequest))]
public Request.BaseRequest Request
{
    get;
    set;
}

element/root 级别

也不需要 Namespace = ""

最后我通过XmlAttributeOverrides解决了问题:

[Serializable()]   
public abstract class BaseRequest
{
...
}

[XmlRoot("EXTSYSTEM", Namespace="")]
public class ResponseMessageEnvelope
{

    [XmlElement("REQUEST")]
    public Request.BaseRequest Request
    {
        get;
        set;
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");

        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();           

        XmlElementAttribute attr = new XmlElementAttribute();
        attr.ElementName = "REQUEST";
        attr.Type = this.Request.GetType();
        attr.Namespace = "";
        attrs.XmlElements.Add(attr);

        attrOverrides.Add(typeof(ResponseMessageEnvelope), "Request", attrs);

        using (XmlWriter sWriter = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true ,Indent = true }))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ResponseMessageEnvelope), attrOverrides);
            serializer.Serialize(sWriter, this, ns);                
            return sb.ToString();
        }
    }