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

Control XML Serialization element name

Hi all,
I want to control element name of a collection in XML serialization,
[Serializable()]
public class MatchedServiceItem {
[XmlArrayItem(ElementName = "ServiceList")]
public List<int[]MatchedServiceIDs;
}

Then I get following result
<?xml version="1.0" encoding="utf-8"?>
<Setting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MatchedServiceItems>
<MatchedServiceItem>
<MatchedServiceIDs>
<ServiceList>
<int>1</int>
<int>2</int>
</ServiceList>
<ServiceList>
<int>1000</int>
</ServiceList>
</MatchedServiceIDs>
<MatchedServiceText>Match A</MatchedServiceText>
</MatchedServiceItem>
</MatchedServiceItems>
</Setting>

Now how can I furtuer control element name of int[] (nested within
List<int[]>)?

Thanks in advance!
--
Regards
Hardy
Apr 11 '07 #1
3 7613
I can't think of any neat ways that don't involve extra classes... but
perhaps reverse engineer from the desired xml via xsd and see what you
get? A quick test shows that repeated XmlArrayItem attributes with
different nest-leves might just do the trick...

Apr 11 '07 #2
Like so? (courtesy of some xsd.exe testing...)

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

[Serializable()]
public class MatchedServiceItem {
[XmlArrayItem(ElementName = "ServiceList", NestingLevel = 0)]
[XmlArrayItem(ElementName = "Val", NestingLevel = 1)]
public List<int[]MatchedServiceIDs;
}
class Program {
static void Main(string[] args) {
MatchedServiceItem msi = new MatchedServiceItem();
msi.MatchedServiceIDs = new List<int[]>();
msi.MatchedServiceIDs.Add(new int[] { 1, 2 });
msi.MatchedServiceIDs.Add(new int[] { 1000 });

XmlSerializer ser = new
XmlSerializer(typeof(MatchedServiceItem));
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb)) {
ser.Serialize(sw, msi);
sw.Close();
}
Console.Write(sb.ToString());
Console.ReadKey();

}
}

Apr 11 '07 #3
Cool, it works like magic!!!
--
Regards
Hardy
"Marc Gravell" wrote:
Like so? (courtesy of some xsd.exe testing...)

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

[Serializable()]
public class MatchedServiceItem {
[XmlArrayItem(ElementName = "ServiceList", NestingLevel = 0)]
[XmlArrayItem(ElementName = "Val", NestingLevel = 1)]
public List<int[]MatchedServiceIDs;
}
class Program {
static void Main(string[] args) {
MatchedServiceItem msi = new MatchedServiceItem();
msi.MatchedServiceIDs = new List<int[]>();
msi.MatchedServiceIDs.Add(new int[] { 1, 2 });
msi.MatchedServiceIDs.Add(new int[] { 1000 });

XmlSerializer ser = new
XmlSerializer(typeof(MatchedServiceItem));
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb)) {
ser.Serialize(sw, msi);
sw.Close();
}
Console.Write(sb.ToString());
Console.ReadKey();

}
}

Apr 12 '07 #4

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

Similar topics

0
by: Alberto Grosso Nicolin | last post by:
We have the following XML schema: there's a root element (Response) with of a single child element (Result). ...
0
by: eSapient | last post by:
I generated serialization/deserialization code for this schema using the xsd tool: <?xml version="1.0" encoding="UTF-8"?> <xs:schema elementFormDefault="qualified"...
4
by: Brian Keating | last post by:
wonder if anyone can help me here, i've a framework 1.1 dataset which i serialize in framework 1.1 and deserialize in framework 2.0. This is fine, problem is that i want to modify some of the...
7
by: Dave | last post by:
Hi all, I know there is a lot of information on here about Serialization, but I can't find what I need so I thought i would ask. I am trying to serialize and deserialize a set of data. ...
1
by: Dave | last post by:
I'm having trouble dynamically deserializing information. The following code is what I have. It serialises fine, the Dave element is serialized as the Name of the element, not its value. On...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
4
by: Sjaakie | last post by:
Hi, I've read some articles on serialization, but I don't see how to serialize the xml-structure below (if it is possible at all). Could you point me in the right direction? TIA <?xml...
0
by: groovyghoul | last post by:
Hi I have the following XML file: =========================================================== <?xml version="1.0" encoding="UTF-16"?> <Policy xmlns="http://tempuri.org/richard.xsd"> <TransType...
2
by: Nishant Mehta | last post by:
Hi all, I am serialiizing a c# object to an XML file to save some application settings. The idea is to ship this xml file along with the application and deserialize after the application is...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.