Night Walker
Night Walker

Reputation: 21280

Part of xml message is not deserialized correctly

Hello I have following deserialization code

        public static T DeserializeXML<T>(String xml) where T : class
        {
            T newObject = null;
            XmlSerializer s = new XmlSerializer(typeof(T));
            using (StringReader sw = new StringReader(xml))
            {
                newObject = (T)s.Deserialize(sw);
            }
            return newObject;
        }

My message that I try to deserialize

<Data>
<ItemIn date="2012-08-09T10:25:54.06+01:00" itemId="000007721" Id="1">   <Extensions><Info Id="parts" order="issue"/></Extensions></ItemIn>
</Data>

But I just never getting Extensions part desirialized back to the original class I always get null there. Rest of the class is ok .

Any suggestions what to check ?

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ItemTransferIn {

    private Extensions extensions;

    private System.DateTime date;

    private string itemId;

    private string Id;

    /// <remarks/>
    public ItemTransferInExtensions Extensions {
        get {
            return this.extensions;
        }
        set {
            this.extensions = value;
        }
    }

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class Extensions {

    private RecipeInfo recipeInfoField;

    /// <remarks/>
    public RecipeInfo RecipeInfo {
        get {
            return this.recipeInfoField;
        }
        set {
            this.recipeInfoField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class RecipeInfo {

    private string recipeIdField;

    private string orderIdField;

    private string itemBarcodeIdField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Id {
        get {
            return this.recipeIdField;
        }
        set {
            this.recipeIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string order {
        get {
            return this.orderIdField;
        }
        set {
            this.orderIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string itemBarcodeId {
        get {
            return this.itemBarcodeIdField;
        }
        set {
            this.itemBarcodeIdField = value;
        }
    }
}

Upvotes: 1

Views: 190

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

For extensions... it looks like you've edited the code; there is

private Extensions extensions;

...

/// <remarks/>
public ItemTransferInExtensions Extensions {
    get {
        return this.extensions;
    }
    set {
        this.extensions = value;
    }
}

Frankly, that shouldn't even compile; we don't have an ItemTransferInExtensions class.

Also, Info won't work:

/// <remarks/>
public RecipeInfo RecipeInfo {...blah...}

Does not match <Info ..../>. So either correct the xsd and regenerate the cs, or correct the xml; but at the moment they do not match.

After renaming the RecipeInfo property to Info (you could also just add an attribute) and fixing the Extensions / ItemTransferInExtensions (and the missing }), and adding a root class to match the xml:

public class Data
{
    public ItemTransferIn ItemIn { get; set; }
}

... it all works fine:

static void Main()
{
    string msg = @"<Data>
<ItemIn date=""2012-08-09T10:25:54.06+01:00"" itemId=""000007721"" Id=""1"">   <Extensions><Info Id=""parts"" order=""issue""/></Extensions></ItemIn>
</Data>";
    var obj = DeserializeXML<Data>(msg);

    Console.WriteLine(obj.ItemIn.Extensions.Info.order); // issue
}

Frankly, though, it is easier to do it manually:

public class Data
{
    public ItemTransferIn ItemIn { get; set; }
}
public class ItemTransferIn
{
    [XmlAttribute("date")]
    public DateTime Date { get; set; }

    [XmlAttribute("itemId")]
    public string Itemid { get; set; }

    [XmlAttribute]
    public int Id { get; set; }

    public Extensions Extensions { get; set; }
}
public class Extensions
{
    public ExtensionsInfo Info { get; set; }
}
public class ExtensionsInfo
{
    public int Id { get; set; }
    [XmlAttribute("order")]
    public string Order { get; set; }
}

Upvotes: 2

Related Questions