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 first class's object with webMethod
:
[WebMethod]
public testClass1 testMethod()
{
testClass1 test = new testClass1();
test.Name = "stackoverflow";
test.testClass2Object = new testClass2(2);
return test;
}
but I don't get the values of testClass2
property from testClass1
object.
I tried [Serializable]
[XmlInclude(typeof(testClass2))]
annotations but nothing changed. Any suggestions?
If I run the code "as is" and invoke 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>
Are you expecting something different? Maybe I'm missing something.
If this is part of a bigger project, maybe try putting just this code into a new project and seeing if it could be a setting or other configuration type of problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With