1

I have one serializable class

[XmlRoot("p")]
public class BatchField
{
    private string _type;
    private string _value;


    [XmlAttribute("type")]
    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }

    [XmlAttribute("value")]
    public string Value
    {
        get { return _value; }
        set { _value = value; }
    }
}

[XmlRoot("Batch")]
public class Batch
{


    private Collection<BatchField> _p;

    [XmlElement]
    public Collection<BatchField> P
    {
        get { return _p; }
        set { _p = value; }
    }

    public Batch()
    {
        _p = new Collection<BatchField>();
    }


}

I am able to serialize and serialize it.After Serialization it generates below XML. This XML is good and m able DE-serialize it successfully.

<?xml version="1.0" encoding="utf-8"?>
<Batch>
  <P type="Test" value="113" />
  <P type="Test1" value="113" />
  <P type="Test2" value="113" />
  <P type="Test3" value="113" />
  <P type="Test4" value="113" />
  <P type="Test5" value="113" />
</Batch>

Now My Question is

I want make this P dynamic. In Some cases it can be Scanner or in Some cases it can be Codec . If i change my batch class I would be able to DE-serialize this class w/o any problem. But I want do it with same class structure...No change in naming ...Is it possible ? Or I am going in wrong direction ? If it is possible then please give some hint...

Summary

I want DE-serialize below XML structures with above given batch class.

<?xml version="1.0" encoding="utf-8"?>
    <Batch>
      <Scanner type="Test" value="113" />
      <Scanner type="Test1" value="113" />
      <Scanner type="Test2" value="113" />
      <Scanner type="Test3" value="113" />
      <Scanner type="Test4" value="113" />
      <Scanner type="Test5" value="113" />
    </Batch>

I am bit new to this concept.

Thanks in Advance SB

  • @laszlokiss88 : Yup I saw that....And in short answer is NO... – Sachin Bankar Nov 09 '15 at 13:06
  • in that case sorry, but I don't see the difference – laszlokiss88 Nov 09 '15 at 13:08
  • @laszlokiss88 41 : LOL ... I wanted to say...There is NO way to make it dynamic...By looking at question you mentioned .... – Sachin Bankar Nov 09 '15 at 13:10
  • You might be better off running the XML through a XSLT to normalize it down to your generic `P`, then you would only need to update the XSLT if you need to add a new type, or you can look into the `XmlAnyElement` attribute. That is, if I understand what you are trying to do. You want to take XML that could have 1 of several element names under `Batch` and shove them all into your `P`? – gmiley Nov 09 '15 at 13:37
  • You can easily use `Linq-to-XML` to solve your problem. – Arghya C Nov 09 '15 at 14:04
  • @SachinBankar There's no way to do it _declaratively_. Certainly you can write methods to make these types of translations by writing your own deserialization code, or you can deserialize to a type that matches the XML directly, and convert to a different type in C# code. – D Stanley Nov 09 '15 at 14:11

0 Answers0