0

Here is the webservice:

Custom classes:

public class A
{
    public A()
    {
    }
    public B prop { get; set; } 
}

public class B
{
    public B()
    {
    }
    public A prop { get; set; }
}

Webmethod:

[WebMethod]
[XmlInclude(typeof(A))]
public object Test()
{
    A a = new A();
    a.prop = new B();

    return a;
}

Here is the client side:

Service ws = new Service();
var response = ws.Test();

So, why is the webservice returning XmlNode list instead of class A? How to solve it?

PS: if I comment public A prop { get; set; } line, it works

4
  • FYI, the [Serializable] attribute is not used by XML Serialization. Commented Mar 26, 2012 at 0:22
  • Alright, removed. Any ideas how to solve this loop serialization problem? Commented Apr 4, 2012 at 14:50
  • There is no problem. You told it to return object, so it's returning the most general possible type. If you didn't mean object, then don't return object. Commented Apr 4, 2012 at 15:53
  • The main point here is that if I comment public A prop { get; set; } it will return A as expected. How to achieve this without commenting code line? Commented Apr 6, 2012 at 0:27

1 Answer 1

1

Ok, found the solution:

I could use a wrapper, like this:

Or I use the [return:] attribute, like this:

[WebMethod]
[return: XmlElement(typeof(A))]
public object Test()
{
    A a = new A();
    a.prop = new B();

    return a;
}

About attribute targets:

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

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.