473,503 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object Serialization

How would I set up a class or classes to serialize to XML in the format
below? I cannot figure out how to serialize the group of links as "id"
attributes of the link element.

<data>
<links>
<link id="1" />
<link id="2" />
</links>
<time>12:00</time>
</data?
Jun 7 '07 #1
1 2154
KittyHawk wrote:
How would I set up a class or classes to serialize to XML in the format
below? I cannot figure out how to serialize the group of links as "id"
attributes of the link element.

<data>
<links>
<link id="1" />
<link id="2" />
</links>
<time>12:00</time>
</data?
Ignoring the time element (which should be clear) you can use e.g.

[XmlRoot(ElementName="data")]
public class Data
{
public Data() { }

[XmlArray(ElementName="links")]
[XmlArrayItem(ElementName="link")]
public Link[] Links;
}

public class Link
{
public Link() { }

[XmlAttribute(AttributeName = "id")]
public string Id;
}

then you get e.g.

Data data = new Data();
Link link1 = new Link();
link1.Id = "1";
Link link2 = new Link();
link2.Id = "2";
data.Links = new Link[] { link1, link2 };

XmlSerializer serializer = new XmlSerializer(typeof(Data));
serializer.Serialize(Console.Out, data);

output as

<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<links>
<link id="1" />
<link id="2" />
</links>
</data>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 7 '07 #2

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

Similar topics

6
7136
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open...
0
2054
by: Philip Reed | last post by:
I'm trying to write a preferences-handling infrastructure that serializes prefs to XML. Basically I want to read in a common "default" prefs set, then read in the user's prefs and override the...
5
7557
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully...
8
12647
by: rawCoder | last post by:
Hi All, I need some advanced samples or references for passing custom objects over the network using sockets. Without using Remoting what are other options in .NET Framework for this binary...
3
2738
by: AVL | last post by:
Hi, I'm new to .net. I need some info on serialization. What is serialization? Why do we need it? Why objects need to be serialized if they need to be stored in session or viewstate?
5
2257
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
1
5525
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
3
2142
by: benkial | last post by:
Below is a custom exception class that I created to be shared by my C+ + and C# code. It works fine till I need to pass the exception object through Remoting: every time a FtException is raized in...
0
1890
by: anchiang | last post by:
Hi All, I have XML: <RegistryResponse status="Success" xmlns="urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1"> <AdhocQueryResponse xmlns="urn:oasis:names:tc:ebxml-regrep:query:xsd:2.1"> ...
3
2111
by: Jeremy | last post by:
I've created a serializable class and put attributes around all the properties that should be serialized. I return the class from a web service, but my problem is that the wsdl for the web service...
0
7204
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,...
0
7091
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
7282
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,...
1
6998
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
7464
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...
1
5018
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
3171
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...
0
1516
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 ...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.