471,317 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

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 objXmlSerializer = new
XmlSerializer(typeof(Entity));
objTextWriter = new StreamWriter(@"C:\Entity.xml");
objXmlSerializer.Serialize(objTextWriter, objEntity);

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

Cheers,
Vin

Jan 17 '06 #1
2 10119
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]
- mv*@spam.guard.caspershouse.com

"Vinayak Kamat" <kg*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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 objXmlSerializer = new
XmlSerializer(typeof(Entity));
objTextWriter = new StreamWriter(@"C:\Entity.xml");
objXmlSerializer.Serialize(objTextWriter, objEntity);

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

Cheers,
Vin

Jan 17 '06 #2
How about modifying your code to:

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

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" <kg*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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 objXmlSerializer = new
XmlSerializer(typeof(Entity));
objTextWriter = new StreamWriter(@"C:\Entity.xml");
objXmlSerializer.Serialize(objTextWriter, objEntity);

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

Cheers,
Vin

Jan 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Uttam | last post: by
4 posts views Thread by Alexander | last post: by
3 posts views Thread by Steve | last post: by
2 posts views Thread by Vinayak Kamat | last post: by
reply views Thread by Kevin Burton | last post: by
5 posts views Thread by Nikola Skoric | last post: by
8 posts views Thread by =?Utf-8?B?UGlnZ3k=?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.