0

I am currently developing a client library for connecting to Newegg using the documentation provided by Newegg and have a question on class design.

In working with various API's ( namely NetSuite and Amazon's MWS ) I come across classes that have are used like this:

 recToFulfill.packageList = new ItemFulfillmentPackageList();
 recToFulfill.packageList.package = new ItemFulfillmentPackage[ifitemlist.item.Length];
 recToFulfill.packageList.package[i] = new ItemFulfillmentPackage();
 recToFulfill.packageList.package[i].packageWeightSpecified = true;
 recToFulfill.packageList.package[i].packageTrackingNumber = "trackingNumber";

The question I have is: How do I properly design the nested objects like above? I have never had to worry about this previously, so I am unsure on where to look, or start.

The bit I need to figure out looks like this ( taken from the API documentation provided):

<UpdateOrderStatusInfo>
    <IsSuccess></IsSuccess>
    <Result>
        <OrderNumber></OrderNumber>
        <SellerID></SellerID>
        <OrderStatus></OrderStatus>
    </Result>
</UpdateOrderStatusInfo>

All fields are type string, except order number which is an integer.

I have this currently:

 public class UpdateOrderStatusInfo
{
    public string IsSuccess { get; set; }
    public int OrderNumber { get; set; }
    public string SellerID { get; set; }
    public string OrderStatus { get; set; }
}

But the returned XML Response has Results as a parent node which to me seems like it should be represented within the class itself. Would I just do this?

public UpdateOrderStatusInfo results {get; set;}

If so, where do the child nodes go?

What I need is to be able to say is something like:

UpdateOrderStatusInfo updateInfo = new UpdateOrderStatusInfo();
if(updateInfo.IsSuccess.Equals("true")
{
     Console.WriteLine(updateInfo.Results.OrderStatus);
}    

Any help, or advice on where to get this information is appreciated.

4
  • 1
    If you have the xml, use the XSD tool to automatically create a matching class. Commented Jul 10, 2012 at 18:45
  • I don't have the XML itself unfortunately - just the developers guide they make available with XML snippits and diagrams Commented Jul 10, 2012 at 18:46
  • Your question would be clearer if your examples weren't talking about two different things. Why are you calling them "child nodes"? Are they nested, i.e. are there Order Statuses within Order Statuses; or do you merely need more than one, as in public UpdateOrderStatusInfo[] results { get; set; } (notice the brackets) Commented Jul 10, 2012 at 18:46
  • 1
    the returned XML Response suggests that you do have some of the XML. Can you post that? Commented Jul 10, 2012 at 18:48

3 Answers 3

1

Easy breezy. If it has no children, it's a scalar property. If it does, it is its own class, and referenced in the parent class accordingly. If it repeats, it's a collection, and is referenced like a class (these are complex type, not primitives). Make sure you initialize them in your constructors).

class Program
{
    static void Main(string[] args)
    {
        var myOrder = new UpdateOrderStatusInfo();
        myOrder.IsSuccess = "true";
        myOrder.OrderResult.OrderNumber = 1001;
        myOrder.OrderResult.OrderStatus = "Pending";
        myOrder.OrderResult.SellerID = "69";
    }
}

public class UpdateOrderStatusInfo
{
    public string IsSuccess { get; set; }
    public Result OrderResult { get; set; }
    public UpdateOrderStatusInfo()
    {
        OrderResult = new Result();
    }
}

public class Result
{
    public int OrderNumber { get; set; }
    public string SellerID { get; set; }
    public string OrderStatus { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to define the Result as a separate class, called whatever you want, then add a Result property as that type. The Result class can be defined at the namespace level, or, if you are unlikely to use it anywhere else on its own, you can nest the class definition inside the UpdateOrderStatusInfo class:

public class UpdateOrderStatusInfo
{
    public class UpdateOrderResult
    {
        public int OrderNumber { get; set; }
        public string SellerID { get; set; }
        public string OrderStatus { get; set; }
    }

    public UpdateOrderStatusInfo()
    {
        Result = new UpdateOrderResult();
    }

    public string IsSuccess { get; set; }
    public UpdateOrderResult Result { get; set; }
}

1 Comment

Thanks for the clear example, and for rewriting the ambiguous naming you had previously :)
0

The easy way is to use the xsd.exe tool.

The command xsd response.xml will generate the file response.xsd The command xsd response.xsd /C will generate the file response.cs which contains the classes necessary to serialize/deserialize the xml posted.

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.