Deserializing XML to class, some elements not not pulling through

Viewed 27

I am having some difficulties figuring out how to correctly structure my classes to mirror the XML that I am attempting to deserialize. Most elements are coming through, but for example, in the XML below the UOMs object is not being deserialized.

Example XML:

    <Items xmlns="http://www.manh.com/ILSNET/Interface" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Item>
        <Desc>Desc Field Example Value</Desc>
        <Item>PE0000009790</Item>
        <ItemCategories>
            <Action>SAVE</Action>
            <Category1>COMPONENT</Category1>
            <Category2>Category2ExampleValue</Category2>
        </ItemCategories>
        <ItemClass>
            <Action>SAVE</Action>
            <ItemClass>Example ItemClass</ItemClass>
        </ItemClass>
        <UOMS>
            <UOM>
                <Action>SAVE</Action>
                <ConvQty>1</ConvQty>
                <DimensionUm>IN</DimensionUm>
                <Height>0.0</Height>
                <Length>0.0</Length>
                <QtyUm>EA</QtyUm>
                <Sequence>1</Sequence>
                <Weight>0</Weight>
                <WeightUm>LB</WeightUm>
                <Width>0.0</Width>
            </UOM>
        </UOMS>
    </Item>
    </Items>

I am using basic XML deserialization code, which works well but just providing for background:

using (FileStream fileStream = new FileStream(Filename, FileMode.Open))
{
    this.CreatedObjects = (ItemList)serializer.Deserialize(fileStream);
}

The first class is below:

[XmlRoot(ElementName = "Items", Namespace = "http://www.manh.com/ILSNET/Interface")]
public class ItemList
{
    [XmlElement("Item")]
    public Item[] Items { get; set; }
}

public class Item
{
    [XmlElement("ItemCategories")]
    public ItemCategory[] Categories { get; set; }

    [XmlElement("ItemClass")]
    public ItemClass[] Classes { get; set; }

    [XmlElement("UOMS")]
    public ItemUOMS UOMs { get; set; }

    [XmlElement("Desc")]
    public string Description { get; set; }

    [XmlElement("Item")]
    public string Id { get; set; }
}

The second class, which I am struggling to populate, is below (this class is a member of the Item class shown above:

[XmlRoot(ElementName = "UOMS", Namespace = "http://www.manh.com/ILSNET/Interface")]
public class ItemUOMS
{
    [XmlElement("UOM")]
    public ItemUOM[] UOM { get; set; }
}

[XmlRoot(ElementName = "UOM", Namespace = "http://www.manh.com/ILSNET/Interface")]
public class ItemUOM
{
    [XmlElement("Action")]
    public string Action { get; }

    [XmlElement("DimensionUm")]
    public string DimensionUnit { get; }

    [XmlElement]
    public decimal Height { get; }

    [XmlElement]
    public decimal Length { get; }

    [XmlElement("QtyUm")]
    public string QtyUnit { get; }

    [XmlElement("ConvQty")]
    public decimal Quantity { get; }

    [XmlElement]
    public int Sequence { get; }

    [XmlElement]
    public decimal Weight { get; }

    [XmlElement("WeightUm")]
    public string WeightUnit { get; }

    [XmlElement]
    public decimal Width { get; }
}

Unfortunately, I don't get an error, but instead the UOMs objects are just default objects without any values being set. I tried to reference some XML to C# Class tools online that are supposed to output C# classes for you based on XML provided to the tool, but their classes didn't seem to solve the issue for me either. I attempted to use [XmlArray] / [XmlArrayItem], but neither had any effect either.

If you need any more information from me, please let me know. Thanks for any assistance/guidance you are able to provide in advance.

0 Answers
Related