473,398 Members | 2,380 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,398 software developers and data experts.

Conditional XML serialization???

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 16 '06 #1
2 3872
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*********************@z14g2000cwz.googlegro ups.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
It works !!!!

Thank you very much Clive.
That's what I was looking for.

Cheers,
Vin

Jan 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
3
by: Hollywood | last post by:
I have an class (A) that contains an class (B1) of type (B). An class (C), contained by class (A), contains its own class (B2) of type (B). When I want to serialize class (A), I want to ignore,...
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...
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;
4
by: a | last post by:
I'm having trouble testing a custom object. I've tried many different approaches. One is shown below. The XML below shows the state of the object and I'm trying to test for that state, ie there...
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)...
0
by: enricolyb | last post by:
hi, i created a xsd schema and corresponding cs file using xsd.exe. now i want to skip individual items inside an array of elements based on an attribute value. e.g. <nodes> <node attr = "1">...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.