Windows Vista Forums

Configuring XML Serialization to not add an element
  1. #1


    Yash Ganthe Guest

    Configuring XML Serialization to not add an element

    Hi,

    I have the follwoing class structure:

    public class MyClass {
    public Fields Fields;
    }

    public partial class Fields {

    public BaseField[] myFields;

    }



    [System.Xml.Serialization.XmlInclude(typeof(DerivedField))]
    public class BaseField
    {
    //some properties
    }

    [XmlRoot("derived")]
    public partial class DerivedField : BaseField {
    }

    All the classes are tagged as serilizable.
    When I XMLSerialize an object of type MyClass, here is what I get:
    <MyClass>
    <Fields>
    <myFields>
    <derived name="JobID" id="A" precision="9" />
    </myFields>
    </Fields>
    </MyClass>

    The serializer adds an XML element for every public property, which is
    why myfields appears as a tag in the XML.
    This is undesirable in my application as I would like the elements in
    myFields[] to get serialized directly under <Fields> as :
    <MyClass>
    <Fields>
    <derived name="JobID" id="A" precision="9" />
    <derived name="JobID" id="B" precision="9" />
    <derived1 name="JobID" id="C" precision="9" />
    <derived2 name="JobID" id="D" precision="9" />
    </Fields>
    </MyClass>

    Is there a way to instruct the serializer using Attributes or some
    other to not add a tag for the myFields array?


    Thanks,
    Yash

      My System SpecsSystem Spec

  2. #2


    Hovhannes Terteryan Guest

    Re: Configuring XML Serialization to not add an element

    Hi,

    I suppose you either need to implement IXmlSerializable in MyClass, or make
    the class Fields as a collection.


    "Yash Ganthe" <yashgt@newsgroup> wrote in message
    news:95a32c1f-df23-4221-a775-fe92a50f3c28@newsgroup

    > Hi,
    >
    > I have the follwoing class structure:
    >
    > public class MyClass {
    > public Fields Fields;
    > }
    >
    > public partial class Fields {
    >
    > public BaseField[] myFields;
    >
    > }
    >
    > [System.Xml.Serialization.XmlInclude(typeof(DerivedField))]
    > public class BaseField
    > {
    > //some properties
    > }
    >
    > [XmlRoot("derived")]
    > public partial class DerivedField : BaseField {
    > }
    >
    > All the classes are tagged as serilizable.
    > When I XMLSerialize an object of type MyClass, here is what I get:
    > <MyClass>
    > <Fields>
    > <myFields>
    > <derived name="JobID" id="A" precision="9" />
    > </myFields>
    > </Fields>
    > </MyClass>
    >
    > The serializer adds an XML element for every public property, which is
    > why myfields appears as a tag in the XML.
    > This is undesirable in my application as I would like the elements in
    > myFields[] to get serialized directly under <Fields> as :
    > <MyClass>
    > <Fields>
    > <derived name="JobID" id="A" precision="9" />
    > <derived name="JobID" id="B" precision="9" />
    > <derived1 name="JobID" id="C" precision="9" />
    > <derived2 name="JobID" id="D" precision="9" />
    > </Fields>
    > </MyClass>
    >
    > Is there a way to instruct the serializer using Attributes or some
    > other to not add a tag for the myFields array?
    >
    >
    > Thanks,
    > Yash


      My System SpecsSystem Spec

  3. #3


    Family Tree Mike Guest

    RE: Configuring XML Serialization to not add an element



    "Yash Ganthe" wrote:

    > Hi,
    >
    > I have the follwoing class structure:
    >
    > public class MyClass {
    > public Fields Fields;
    > }
    >
    > public partial class Fields {
    >
    > public BaseField[] myFields;
    >
    > }
    >
    > [System.Xml.Serialization.XmlInclude(typeof(DerivedField))]
    > public class BaseField
    > {
    > //some properties
    > }
    >
    > [XmlRoot("derived")]
    > public partial class DerivedField : BaseField {
    > }
    >
    > All the classes are tagged as serilizable.
    > When I XMLSerialize an object of type MyClass, here is what I get:
    > <MyClass>
    > <Fields>
    > <myFields>
    > <derived name="JobID" id="A" precision="9" />
    > </myFields>
    > </Fields>
    > </MyClass>
    >
    > The serializer adds an XML element for every public property, which is
    > why myfields appears as a tag in the XML.
    > This is undesirable in my application as I would like the elements in
    > myFields[] to get serialized directly under <Fields> as :
    > <MyClass>
    > <Fields>
    > <derived name="JobID" id="A" precision="9" />
    > <derived name="JobID" id="B" precision="9" />
    > <derived1 name="JobID" id="C" precision="9" />
    > <derived2 name="JobID" id="D" precision="9" />
    > </Fields>
    > </MyClass>
    >
    > Is there a way to instruct the serializer using Attributes or some
    > other to not add a tag for the myFields array?
    >
    >
    > Thanks,
    > Yash
    > .
    >
    You can use XmlIgnore as in the following:

    public partial class Fields
    {
    [XmlIgnore()]
    public BaseField[] myFields;
    }


    Mike

      My System SpecsSystem Spec

Configuring XML Serialization to not add an element problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialization optimization guy1976 .NET General 3 11 Dec 2008
WCF Custom Serialization Vikas Indigo 0 04 Jun 2008
XML Serialization Tom .NET General 5 16 Apr 2008
Serialization Reference Alhambra Eidos Kiquenet Indigo 1 31 May 2007
Serialization frameworks on .net 3.0 Robin Davies WinFX General 0 13 Nov 2006