Conditional XML serialization (Runtime)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vinayak  Kamat

    #1

    Conditional XML serialization (Runtime)

    Hi,

    I've got two classes - Entity which will have an array of Child objects
    in it and Child class.

    class Entity
    {

    public Child[] Children;

    }

    class Child
    {

    public string Name;
    public string Description;

    }

    I want to be able to serialize this class into two different XMLs.

    1) looks like,
    <Entity>
    <Child Name="" Description=""/>
    <Child Name="" Description=""/>
    <Child Name="" Description=""/>
    </Entity>

    2) looks like,
    <Entity>
    <Child Name=""/>
    <Child Name=""/>
    <Child Name=""/>
    </Entity>

    How to do this? Coz If I use [XmlIgnore] attribute on class Child's
    description attribute, then it won't allow me to produce Xml (1).
    So its basically to control serialization during runtime.

    My serialization code currently looks like,

    // Serialize the Entity object in to an XML document, and we are done.
    TextWriter objTextWriter = null;
    XmlSerializer objXmlSerialize r = new
    XmlSerializer(t ypeof(Entity));
    objTextWriter = new StreamWriter(@" C:\Entity.xml") ;
    objXmlSerialize r.Serialize(obj TextWriter, objEntity);

    Any help in this regard would be of great help. MS experts help me out
    here... please..

    Cheers,
    Vin

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Conditional XML serialization (Runtime)

    Vinayak,

    You are going to have to use a separate code path in order to do this.
    The XML serializer does not have a way of acting on variable data that you
    feed it (in the sense of flags to control attributes which control
    serialization).

    The easiest thing to do is to not ignore the description property, and
    allow it to be serialized. Then, you can run the document through a DOM
    document, and remove the description attributes, and then pass that on.

    I am curious though, for what need do you have for two separate XML
    formats? I don't see a reason why the description element can't just be
    ignored.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Vinayak Kamat" <kgvinayak@gmai l.com> wrote in message
    news:1137457417 .516128.145610@ g47g2000cwa.goo glegroups.com.. .[color=blue]
    > Hi,
    >
    > I've got two classes - Entity which will have an array of Child objects
    > in it and Child class.
    >
    > class Entity
    > {
    >
    > public Child[] Children;
    >
    > }
    >
    > class Child
    > {
    >
    > public string Name;
    > public string Description;
    >
    > }
    >
    > I want to be able to serialize this class into two different XMLs.
    >
    > 1) looks like,
    > <Entity>
    > <Child Name="" Description=""/>
    > <Child Name="" Description=""/>
    > <Child Name="" Description=""/>
    > </Entity>
    >
    > 2) looks like,
    > <Entity>
    > <Child Name=""/>
    > <Child Name=""/>
    > <Child Name=""/>
    > </Entity>
    >
    > How to do this? Coz If I use [XmlIgnore] attribute on class Child's
    > description attribute, then it won't allow me to produce Xml (1).
    > So its basically to control serialization during runtime.
    >
    > My serialization code currently looks like,
    >
    > // Serialize the Entity object in to an XML document, and we are done.
    > TextWriter objTextWriter = null;
    > XmlSerializer objXmlSerialize r = new
    > XmlSerializer(t ypeof(Entity));
    > objTextWriter = new StreamWriter(@" C:\Entity.xml") ;
    > objXmlSerialize r.Serialize(obj TextWriter, objEntity);
    >
    > Any help in this regard would be of great help. MS experts help me out
    > here... please..
    >
    > Cheers,
    > Vin
    >[/color]


    Comment

    • Clive Dixon

      #3
      Re: Conditional XML serialization (Runtime)

      How about modifying your code to:

      class Child
      {
      public string Name;
      public string Description;
      [XmlIgnore]
      public bool DescriptionSpec ified;
      }

      The framework serialization process checks for a Boolean field with the same
      name as each field but suffixed with "Specified" . If such a Boolean field
      exists, it will only serialize the original field if the Boolean field's
      value is 'true'.

      "Vinayak Kamat" <kgvinayak@gmai l.com> wrote in message
      news:1137457417 .516128.145610@ g47g2000cwa.goo glegroups.com.. .[color=blue]
      > Hi,
      >
      > I've got two classes - Entity which will have an array of Child objects
      > in it and Child class.
      >
      > class Entity
      > {
      >
      > public Child[] Children;
      >
      > }
      >
      > class Child
      > {
      >
      > public string Name;
      > public string Description;
      >
      > }
      >
      > I want to be able to serialize this class into two different XMLs.
      >
      > 1) looks like,
      > <Entity>
      > <Child Name="" Description=""/>
      > <Child Name="" Description=""/>
      > <Child Name="" Description=""/>
      > </Entity>
      >
      > 2) looks like,
      > <Entity>
      > <Child Name=""/>
      > <Child Name=""/>
      > <Child Name=""/>
      > </Entity>
      >
      > How to do this? Coz If I use [XmlIgnore] attribute on class Child's
      > description attribute, then it won't allow me to produce Xml (1).
      > So its basically to control serialization during runtime.
      >
      > My serialization code currently looks like,
      >
      > // Serialize the Entity object in to an XML document, and we are done.
      > TextWriter objTextWriter = null;
      > XmlSerializer objXmlSerialize r = new
      > XmlSerializer(t ypeof(Entity));
      > objTextWriter = new StreamWriter(@" C:\Entity.xml") ;
      > objXmlSerialize r.Serialize(obj TextWriter, objEntity);
      >
      > Any help in this regard would be of great help. MS experts help me out
      > here... please..
      >
      > Cheers,
      > Vin
      >[/color]


      Comment

      Working...