473,410 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 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 10366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Philipp Sumi | last post by:
Hello newsgroup I'm using XML serialization controlled over attributes to serialize a couple of simple hierarchical objects into XML. My problem is, that I have a couple of elements within the...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
4
by: Alexander | last post by:
There was such problem, At compilation with SP1 (dotnetfx11SP1) On computers without him{it} ÎÅÄÅÓÅÒÉÁÌÉÚÕÀÔÓÑ some objects. For example (System. Collections. Comparer) and together with...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
3
by: Steve | last post by:
I've been following a couple remoting tutorials on the web, they are all pretty much the same. I've got my different applications built(client, server and remote object (dll)) The client is...
2
by: Vinayak Kamat | last post by:
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;
0
by: Kevin Burton | last post by:
I have defined the following property within a class: <XmlElement(ElementName:="qtyonhand")_ Public Property Quantity() As Integer Get Return _quantity End Get Set(ByVal value As Integer)...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
8
by: =?Utf-8?B?UGlnZ3k=?= | last post by:
Hi to all, I am getting this System.OutOfMemoryException calling the Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream>,<Obj>) method. The type of <streamis...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.