0

Please, is any convenience way to avoid namespace attributes for nested member Request when serializing ResponseMessageEnvelope class? Result xml will not be used for deserialization.

[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();
        }
    }    
}

I tried empty namespace for XmlRoot attribute on base and derived class, also for XmlElement on member but without success. Thanks for advice.

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

This I would like to have:

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

2 Answers 2

1

The base class property must be decorated with the know types to avoid those declarations in the output. Change the request attribute inside ResponseMessageEnvelope to

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

Also Namespace = "" is not needed at the element/root level

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it's working fine for case that there is only one derived class (AuthenticateRequest ) but I have more like BetRequest, ResultRequest .... and all of them have to be serialised as REQUEST in default namespace. I can have same element names but different namespaces (several ELEMENT attributes like stackoverflow.com/questions/15455786/…) or different element name in one namespace (XmlInclude attribute). It give sense for deserialisation process. So thanks accepting as answer.
0

Finally I solve problem by 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();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.