1

This is my sample XML:

<outbound>
      <leg0 Code="aaa" RetCode="ccc"/>
      <leg1 Code="bbb" RetCode="ddd"/>
</outbound>

Is it possible to use somehow XmlElement (I'm using XmlSerializer) to deserialize leg0 and leg1 to one object type?

My object's looks like that:

  public class FlightInfo
  {
    [XmlElement(ElementName = "outbound")]
    public Outbound Outbound { get; set; }
  }

  public class Outbound
  {
    [XmlElement(ElementName = "leg")]
    public List<Leg> Leg { get; set; }
  }

  public class Leg
  {
    [XmlAttribute(AttributeName = "Code")]
    public string Code{ get; set; }
    [XmlAttribute(AttributeName = "RetCode")]
    public string ReturnCode{ get; set; }
  }

My problem is to deserialize elements leg0 and leg1 to Leg object. Can anybody give my a hint?

Mateusz
  • 116
  • 1
  • 9
  • You can always deserialize manually. Read the XML, and for each leg[x] element, create a Leg. – Crowcoder Jan 21 '15 at 14:07
  • I know that I can deserialize manually, but the question is about XmlSerializer. Presented sample is just a small piece of all XML. – Mateusz Jan 21 '15 at 14:16
  • Someone asked just about the same question with no good answer there either. Maybe you can implement a custom XmlAttribute that uses a regex on ElementName instead of a constant expression. http://stackoverflow.com/questions/2874680/xmlserializer-deserialize-different-elements-as-collection-of-same-element – Crowcoder Jan 21 '15 at 14:44

2 Answers2

2

Having every leg with a different element name is the problem here. If its possible could you instead do something like:

<outbound>
  <leg Index=0 Code="aaa" RetCode="ccc"/>
  <leg Index=1 Code="bbb" RetCode="ddd"/>
</outbound>

And the issue goes away.

If you're stuck with the format could you do some "preprocessing". Search and replace "legn" with "leg Index=\"n\"" which would produce the format above that you could then deserialize.

The problem you have as it is, is that when C# Serializes a Leg it needs to know which node name to give - how will it know? You've lost the index.

Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
0

I don't think you can deserialize different element names to the same property. What you could do is have a separate property which would join both lists, so you have an easier way to access it. Something as follows (not tested but should work):

public class Outbound {

[XmlElement(ElementName = "leg0")]
public List<Leg> leg0 { get; set; }

[XmlElement(ElementName = "leg1")]
public List<Leg> leg1 { get; set; }

[XmlIgnore] //You don't want to deserialize this property
public List<Leg> AllLegs { 
get
    {
        return leg0.Join(leg1);
    }
                          }

}

Lee
  • 586
  • 3
  • 5
  • 1
    Thank You for answer, but Your solution doesn't help me, because I write just a simple example. In real case it can be even 10000 legs, so writing 10000 properties doesn't make sense. – Mateusz Jan 21 '15 at 14:13
  • leg0 and leg1 there are just single elements, not lists. – Mateusz Jan 21 '15 at 14:19
  • Well, in that case you could modify the XML prior to deserializing. You could remove all the numbers serving as a suffix to your leg elements, so that they all map to the same leg property. Or else, since this doesn't really fit XML Serializer, you could always use an XML Processor such as XMLDocument or XDocument. Hope this helps. – Lee Jan 21 '15 at 14:49