1

I have the following class:

public class AddCouponInfoRequest : namespace.Request 
{

}

I have an instance of AddCouponInfoRequest in my hand and I want to get an instance of namespace.Request with the same values.

This doesn't work fine:

namespace.Request req = (namespace.Request)request;
string xml = req.SerializeToXml();

The value of xml after serialization is:

<AddCouponInfoRequest xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n \r\n \r\n ...

I need a pure namespace.Request object. What is the best way to do this?

Thanks in advance,

xkcd
  • 2,538
  • 11
  • 59
  • 96

1 Answers1

1

SerializeToXml is a virtual method so it is logical it always calls the overriden method.

You can, for example, create a new method for AddCouponInfoRequest

string SerializeToXmlAsParent()
{
    return base.SerializeToXml();
}
ElDog
  • 1,230
  • 1
  • 10
  • 21
  • In fact I need the instance of the Request object. I did the serialization just to show the conversion didn't work as I expected. – xkcd Jan 25 '12 at 12:03
  • 1
    The serialization in your example works exactly as it should. I'm afraid there is no simple one line of code-way to ignore the overriden virtual method. In your case you have to whether use the method I proposed and then to use the object as Request or you can create some variant of Request.BasicSerialize() method and override it in AddCouponInfoRequest to call base.SerializeToXml() Then you don't have to cast your base class to AddCouponInfoRequest. You fight with a virtual method using virtual method :-) – ElDog Jan 25 '12 at 12:09