Skip object with complex property with web service

I have 2 classes:

    public class testClass1
    {
        public string Name { get; set; }
        public testClass2 testClass2Object { get; set; }
    }

    public class testClass2
    {
        public testClass2() { }

        public testClass2(int i) { TestProperty = i; }

        public int TestProperty { get; set; }
    }

and I want to return a first class object with webMethod:

    [WebMethod]
    public testClass1 testMethod()
    {
        testClass1 test = new testClass1();
        test.Name = "stackoverflow";
        test.testClass2Object = new testClass2(2);
        return test;
    }

but I do not get the property value of the testClass2object testClass1.

I tried annotations [Serializable] [XmlInclude(typeof(testClass2))]but nothing has changed. Any suggestions?

+5
source share
2 answers

If I run the code "as is" and calls testMethod (), I get ...

<testClass1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
     <Name>stackoverflow</Name>
     <testClass2Object>
          <TestProperty>2</TestProperty>
     </testClass2Object>
</testClass1>

Do you expect anything else? Maybe I missed something.

If this is part of a larger project, perhaps try putting that particular code in a new project and see if it could be a parameter or another type of problem configuration.

+1
source

, , . xml- testclass2.

Web API ASMX, SOAP no-schema XML .

Web-API , JSON, XML. !

0

All Articles