views:

200

answers:

0

I'm working with data consumed from a RESTful service that looks like the following:

<?xml version="1.0" encoding="UTF-8"?>
<couponfeed>
  <link type="TEXT">
    ...
  </link>
  <link type="TEXT">
    ...
  </link>
  ...
</couponfeed>

I have the xsd, and I generated C# classes from it using the xsd.exe utility. Here is the relevant part of the generated code:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.linkshare.com/namespaces/couponfeed-1.0")]
[System.Xml.Serialization.XmlRootAttribute("couponfeed", Namespace="http://www.linkshare.com/namespaces/couponfeed-1.0", IsNullable=false)]
public partial class couponfeedType {

    private object[] itemsField;

    [System.Xml.Serialization.XmlElementAttribute("link", typeof(linkType))]
    [System.Xml.Serialization.XmlElementAttribute("network", typeof(networkContainerType))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

It seems to me that when I deserialize at the root level, my couponfeedType object's Items collection should contain linkType objects corresponding to the link nodes. But the Items collection is always null. To demonstrate, I set up some tests with test data that contains 3 link nodes:

    [TestMethod]
    public void TestDataValidatesAgainstSchema() {
        // details removed, but test data validates against schema.
    }

    [TestMethod]
    public void TestDataHasExpectedChildNodes() {
        var xml = new XmlDocument();
        xml.Load(GetTestData());
        Assert.AreEqual(3, xml.SelectNodes("/couponfeed/link").Count);
        // pass
    }

    [TestMethod]
    public void TestDataDeserializes() {
        var serializer = new XmlSerializer(typeof(XmlSchemas.couponfeedType), new XmlRootAttribute("couponfeed"));
        using (var reader = XmlReader.Create(GetTestData())) {
            Assert.IsTrue(serializer.CanDeserialize(reader));
            // ok so far
            XmlSchemas.couponfeedType feed = (XmlSchemas.couponfeedType)serializer.Deserialize(reader);
            Assert.IsNotNull(feed.Items);
            // fail!
        }
    }

Any idea what's happening here? Are there any relevant details I'm not providing? Thanks in advance!