473,569 Members | 2,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="re d" />
</Dog>

What I would like:

<Dog collarColor="re d" />

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.WriteLi ne(Serialize(Ne wDog()));
}

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

private string Serialize(objec t entity)
{
XmlSerializer serializer = new XmlSerializer(e ntity.GetType() );
StringBuilder sb = new StringBuilder() ;
StringWriter sw = new StringWriter(sb );
serializer.Seri alize(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 1180

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

Similar topics

5
2646
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, e.g. class param { object value; }
7
4345
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 behavior at runtime to serialize the properties as attributes. I have been exploring the XMLAttributesOverrides classes and examples which...
3
2063
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 accessors. e.g: public class MyObject { public string name; // This is serialized OK
7
3533
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 TypeConverter other than ReferenceConverter to be put in viewstate." I've included the <Serializable()> attribute, but I'm still getting the same...
5
8013
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 example of what I'd want to accomplish. Thanks Brad Example
0
1494
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 error reflecting type 'Eatec.R5000.Report.ReportControl'" ....the InnerException states:
3
4782
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 processes the posted xml file by reading the data into a C# class object that was generated by XSD.EXE. The class was generated from the...
4
2246
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 XMLTextWriter - e.g. Dim xwriter = New XmlTextWriter("C:\Projects\Formflo\WindowsService\NDde\test.xml", System.Text.Encoding.UTF8)...
8
1991
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 NewsArticle(); NewsArticle.Body = "This is a test news article..."; NewsArticle.Title = "Testing XML";
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.