views:

399

answers:

1

My "Location" object isnt getting serialized in my WCF datacontract, however, all other variables are being set properly. When I try to output a variable in the location object I get the "Object reference not set to an instance of an object" error

My DataContract:

[DataContract(Namespace = "")]
public class CalcRequest : BaseRequest
{
    [DataMember(Name = "Products")]
    public List<Product> products;

    [DataMember(Name = "Location")]
    public Location location;

    [DataMember(Name = "ShippingMethod")]
    public string shippingMethod;

    [DataMember(Name = "SystemPromotionCode")]
    public string systemPromotionCode;

    [DataMember(Name = "UserPromotionCode")]
    public string userPromotionCode;
}

The "Location" object:

[DataContract(Name = "Location", Namespace = "")]
public class Location
{
    public Location()
    {
        // do nothing
    }

    [DataMember(Name = "Country")]
    public string country;

    [DataMember(Name = "StateProvince")]
    public string stateProvince;

    [DataMember(Name = "PostalCode")]
    public string postalCode;
}

my XML request (version, msgtype, processorID, and customerid are in my "BaseRequest"):

<root>
    <Version>1.0</Version>
    <MsgType>type</MsgType>
    <ProcessorId>28000</ProcessorId>
    <CustomerId>28000</CustomerId>
    <Products>
        <Product>
            <SKU>1</SKU>
            <Price>2999</Price>
            <ProductName>name1</ProductName>
            <Quantity>1</Quantity>
        </Product>
        <Product>
            <SKU>2</SKU>
            <Price>1999</Price>
            <ProductName>name2</ProductName>
            <Quantity>1</Quantity>
        </Product>
    </Products>
    <Location>
        <Country>US</Country>
        <StateProvince>OH</StateProvince>
        <PostalCode>44060</PostalCode>
    </Location>
    <ShippingMethod>USPS-NextDay</ShippingMethod>
    <SystemPromotionCode>CD1244578</SystemPromotionCode>
    <UserPromotionCode>2FDGRR</UserPromotionCode>
</root>

... Not sure why this isn't working... any help would be appreciated.

+1  A: 

I don't understand what you think is missing, really....

(stuff deleted - not relevant)

UPDATE: to make sure the order of the elements in the XML is correct and interpreted in the right order, you might want to add Order=xxx statement to the data member attributes-

Otherwise, the data contract serializer will serialize (and deserialize) in alphabetical order (other than the XmlSerializer which serializes in the order the fields appear).

If you have multiple elements of the same order (that's not a problem), then they'll be serialized alphabetically within their order (e.g. all elements of Order=1 will be serialized in an alphabetical fashion - then all elements with Order=2 and so on).

marc_s
I guess I was hoping someone could spot a blatant problem. When I try to Debug.WriteLine(request.location.country); it says the "Object reference not set to an instance of an object"
Chris Klepeis
So when you look at the objects in that WriteLine function, while in debug mode, which one is null?
Tony
@marc_s Yes, the process of converting my xml into an object seems to be the issue... but just for the Location object, since I can access all other objects
Chris Klepeis
@Chris: "serializing" is typically used for object --> XML while deserializing is used for XML --> object - that's why your original question title was a bit confusing (at least to me).
marc_s
Adding an "Order" fixed it. Thanks! I didn't realize it was alphabetical.
Chris Klepeis