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

时间:2015-04-21 15:03:18

标签: c# .net xml-serialization

请序列化 ResponseMessageEnvelope 类时,是否有任何方便的方法来避免嵌套成员请求的命名空间属性?结果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属性尝试了空命名空间,也为成员上的XmlElement尝试了空命名空间但没有成功。谢谢你的建议。

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

我希望:

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

2 个答案:

答案 0 :(得分:1)

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

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

Namespace =&#34;&#34;在元素/根级别不需要

答案 1 :(得分:0)

最后我通过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();
        }
    }