473,379 Members | 1,355 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,379 software developers and data experts.

How to serialize encapsulated class as attributes?

Hello All,

I have a custom class (Collar) that is a property of another custom
class (Dog) which inherits from (Animal).

I want to serialize Dog and have the properties from Collar appear as
attributes on the Dog xml element.

What I have now:

<Dog>
<Collar collarColor="red" />
</Dog>

What I would like:

<Dog collarColor="red" />

Code sample:

class Animal {
public string Name;
}
class Collar {
public string Color;
}
class Dog : Animal {
//[XmlAttribute()] <-- throws an exception that a complex type can't
be an attribute.
public Collar MyCollar = new Collar();
}
class Kennel {
public Kennel() {
Console.WriteLine(Serialize(NewDog()));
}

public Dog NewDog() {
Dog dog = new Dog();
dog.Name = "Fido";
dog.MyColor.Color = "red";
return dog;
}

private string Serialize(object entity)
{
XmlSerializer serializer = new XmlSerializer(entity.GetType());
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
serializer.Serialize(sw, entity);
string xml = sb.ToString();
return xml;
}
}

I want to use Collar on more classes than just Dogs and/or Animals,
which is why it's in its own class. But I want the properties of Collar
to appear as xml attributes of any class I include it in to re-use the
same code in several places.

Any suggestions?

Thanks,
Roger

Dec 12 '06 #1
0 1168

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

Similar topics

5
by: Michel | last post by:
Hi there, What is the best way to serialize unknown data? I have a class that contains a list of parameter objects. The parameter has a value which can be a simple value of a complex class,...
7
by: grwalker | last post by:
I have some classes that have the <Serializable()> attribute applied, which of course by default serializes the class properties as elements. What I would like to do is to be able to override this...
3
by: Peter Cresswell | last post by:
Hello everyone, I would like to serialize an object to XML. Currently my code will serialize all of the public properties that are value types, but not my public properties that have get...
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
5
by: Brad | last post by:
I would like to serialize an arraylist of objects to xml so I can store the xml in a database column. How would I code the serializing and deserializing? Below is a (overly) simple, incomplete...
0
by: elziko | last post by:
I have a complex class that inherits from System.Windows.Forms.UserControl in VS2005. I am trying to serialize this class but I just get an InvalidOperationException stating: "There was an...
3
by: Jeff Richardson | last post by:
This is a repost from the InfoPath news group. Hi, I am writing a SharePoint application that works with InfoPath forms. When a user submits a completed InfoPath form to a forms library my code...
4
by: teddysnips | last post by:
I would like to serialize a class to XML. The format of the XML is fixed (see below). It only needs to be serialized - it is read by another application. I know I can do it manually using...
8
by: Andy B | last post by:
I have the following code in a default.aspx web form page_load event. There seems to be a problem with line 5 (NewsArticle.Date = line). //create a news article NewsArticle NewsArticle = new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...

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.