public class MemberDetail { public string csName = ""; public string csCompany = ""; public string csAddress = ""; public DateTime tsEntered = DateTime.Now; } public class CreditCard { public string csName = ""; public string csNumber = ""; public string csExpiry = ""; } public class ResponseObject // this is returned from the Web Method { public int ErrorCode = 0; public string ErrorText = ""; public string SessionID = ""; // I want to avoid "hard coding this at design time, hence a Dictionary" public CreditCard CCard = new CreditCard(); public MemberDetail MDetail = new MemberDetail();and the Web Method:
[WebMethod] // here we are returning a Serializable object not a string. public ResponseObject GetResp() { // create the CreditCard object and fill it with values CreditCard cc = new CreditCard(); cc.csExpiry = "22/11/2007"; cc.csName = "Gary Wynne"; cc.csNumber = "2233 3242 3323 1211"; // create the MemberDetail object and fill it with values MemberDetail mo = new MemberDetail(); mo.csAddress = "23 Fleet Street"; mo.csCompany = "Newspapers Corp"; mo.csName = "John Smith"; // create the ResponseObject object and fill it with values ResponseObject ro = new ResponseObject(); ro.ErrorCode = 0; ro.ErrorText = "No Error Here"; ro.SessionID = "1234abcd"; ro.MDetail = mo; ro.CCard = cc; return ro; }This gives me output as I need it:
<?xml version="1.0" encoding="utf-8" ?> - <ResponseObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost/"> <ErrorCode>0</ErrorCode> <ErrorText>No Error Here</ErrorText> <SessionID>1234abcd</SessionID> - <CCard> <csName>Gary Wynne</csName> <csNumber>2233 3242 3323 1211</csNumber> <csExpiry>22/11/2007</csExpiry> </CCard> - <MDetail> <csName>John Smith</csName> <csCompany>Newspapers Corp</csCompany> <csAddress>23 Fleet Street</csAddress> <tsEntered>2007-07-29T14:42:40.484375+10:00</tsEntered> </MDetail> </ResponseObject>So is this possible using a Dictionary, where I can do code like this below?
[WebMethod] public object GetMember() { // create the main class for adding props propertywrapper wrapper = new propertywrapper(); // Step 1 - call the AddProperty() to add the 3 main objects // ResponseObject is always returned, the others if needed. ResponseObject ro = new ResponseObject(); wrapper.Addproperty("ResponseObject", ro); //wrapper.Addproperty("ErrorTExt", ""); //wrapper.Addproperty("SessionID", ""); // Step 2 - instantiate the MemberDetails class if needed MemberDetail mdetail = new MemberDetail(); wrapper.Addproperty("MemberDetail", mdetail); // Step 3 - instantiate and add the ccard class if needed // CreditCard cc = new CreditCard(); // wrapper.Addproperty("CredirCard", cc); // return the dictionary values Dictionary<string, object> fullobject = wrapper.GetProps(); return fullobject; }and return an Object rather than a string/xmlstring.