1

In C# I would like to deserialize some Xml where the relative location is important. The following Xml is from a book standard called Onix:

<Stock>
  <OnHand>1</OnHand>
  <Proximity>xx</Proximity>
  <Reserved>2</Reserved>
  <Proximity>yy</Proximity>
  <OnOrder>3</OnOrder>
  <Proximity>zz</Proximity>
  <Cbo>4</Cbo>
  <Proximity>zz</Proximity>
</Stock>

As you can see every 2nd line is called "Proximity". These fields goes with the field above.

If all fields were mandatory then it would be no problem, and the code would look like this:

[XmlElement("OnHand", Order = 0)]public int OnHand { get; set; }
[XmlElement("Proximity", Order = 1)] public string OnHandProximity { get; set; }

[XmlElement("Reserved", Order = 2)] public int Reserved { get; set; }
[XmlElement("Proximity", Order = 3)] public string ReservedProximity { get; set; }

[XmlElement("OnOrder", Order = 4)] public int OnOrder { get; set; }
[XmlElement("Proximity", Order = 5)] public string OnOrderProximity { get; set; }

[XmlElement("CBO", Order = 6)] public int Cbo { get; set; }
[XmlElement("Proximity", Order = 7)] public string CboProximity { get; set; }

But the 4 proximity fields are tightly bound to the field before, and each pair of fields are not mandatory. E.g. you can get xml where the first 2 lines are missing.

Are there any Attributes intended for for these kinds of problems?

Thomas Koelle
  • 3,416
  • 2
  • 23
  • 44
  • 1
    that xml is rubbish. sorry to say but you will have endless troubles with it. prorbaly should use xml atributes or a better structure. to bo honest.. just dont use xml – Piotr Kula Feb 26 '19 at 12:53
  • @ppumkin The xml is what everybody working with books consider the gold standard. it is made by https://www.editeur.org 99% of the format is really fine, but this small part is rubbish. – Thomas Koelle Feb 26 '19 at 12:55
  • 1
    Then that part of the XML is not following the "gold" standard. Because I am looking at it and there is no correlation to the nodes. the XML could come in at randomly ordered... there is just no way to ascociate any of the duplicate nodes with any of the other ones. Hence I say - probably should look more like `1` as it seems to be a property of the OnHand Node- That makes more sense, is easier to validate and parse. The promixty could be a property of other nodes too. It all depends on the data structure. Currently that looks like an array of magic – Piotr Kula Feb 26 '19 at 13:08
  • 1
    A small note. I sadly cannot change this horrible xml because it is the exchange format that is used between different companies. I have added the onix tag. – Thomas Koelle Feb 26 '19 at 13:13
  • I knew you were going to say that. I have been there. Looks like you gonna have to make some magic code to get it working. With C# I would take that NODE XML, put it into a processor that makes sure the nodes are in the correct order - apply some rules if they missing, etc etc. Return real XML using Attributes and make your domain model compliant. But you will probably have to do the reverse to. So your Domain model will be correct but the edge will consume and emit that garbage as if nothing happened. Good luck – Piotr Kula Feb 26 '19 at 13:15

1 Answers1

1

You were on your way with the Order attributes.
Mark the ints as int? and all fields as Nullable:

    [XmlElement("OnHand", Order = 0, IsNullable = true)]
    public int? OnHand { get; set; }

    [XmlElement("Proximity", Order = 1, IsNullable = true)]
    public string OnHandProximity { get; set; }

XmlSerializer should be able to deal with this.

H H
  • 263,252
  • 30
  • 330
  • 514